@devries-cameron20 / t3code-1 / commits / 11df0cbc4a
Fork 0

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

perf(web): pull request comments reuse their markdown parse (#27808)

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

Julius Marminge committed Oct 30, 2026 at 22:10 UTC 11df0cbc4a3726ca8ac4017cf6918329b91ad879
9 files changed +87 -35
apps/web/src/components/ChatMarkdown.browser.test.tsx
+15
@@ -165,6 +165,21 @@ it("reuses the parse of a streaming reply's finished blocks when it remounts", a
165 expect(parsedSources.filter((source) => source.includes("Finished block"))).toHaveLength(1);
166 });
167
168 +it("reuses the parse of a remounted message that has stable extra plugins", async () => {
169 + setup();
170 + const parsed = vi.fn();
171 + const countingPlugins = [() => () => void parsed()];
172 + const message = (
173 + <ChatMarkdown cwd="/tmp/project" text="Cached text" extraRemarkPlugins={countingPlugins} />
174 + );
175 + {
176 + await using _first = await render(message);
177 + }
178 + await using screen = await render(message);
179 + await expect.element(screen.getByText("Cached text")).toBeVisible();
180 + expect(parsed).toHaveBeenCalledTimes(1);
181 +});
182 +
183 describe("ChatMarkdown favicon privacy", () => {
184 it("suppresses private link images while preserving public links across updates", async () => {
185 setup();
apps/web/src/components/ChatMarkdown.tsx
+18 -9
@@ -241,6 +241,7 @@ interface ChatMarkdownProps {
241 imageBaseDir?: string | undefined;
242 workspaceMutationId?: string | null | undefined;
243 onImageExpand?: ((preview: ExpandedImagePreview) => void) | undefined;
244 + /** Keep one list per configuration (a module constant): it keys the shared parse cache. */
245 extraRemarkPlugins?: NonNullable<ReactMarkdownOptions["remarkPlugins"]>;
246 renderContextReference?: ((reference: ChatMarkdownContextReference) => ReactNode) | undefined;
247 /** Loads media a source control host keeps behind a credential through the server, which holds
@@ -1647,6 +1648,18 @@ const CHAT_MARKDOWN_REMARK_PLUGIN_SETS = {
1648 mathBreaks: [REMARK_MATH, ...CHAT_MARKDOWN_REMARK_PLUGINS_WITH_BREAKS],
1649 } satisfies Record<string, PluggableList>;
1650
1651 +const combinedPlugins = new WeakMap<PluggableList, WeakMap<PluggableList, PluggableList>>();
1652 +
1653 +// One combined list per pair, so a stable extra list shares a renderer and its parse cache.
1654 +function withExtraPlugins(shared: PluggableList, extra: PluggableList): PluggableList {
1655 + if (extra.length === 0) return shared;
1656 + let byShared = combinedPlugins.get(extra);
1657 + if (!byShared) combinedPlugins.set(extra, (byShared = new WeakMap()));
1658 + let combined = byShared.get(shared);
1659 + if (!combined) byShared.set(shared, (combined = [...shared, ...extra]));
1660 + return combined;
1661 +}
1662 +
1663 const markdownRenderers = new WeakMap<
1664 PluggableList,
1665 WeakMap<PluggableList, ReturnType<typeof createMarkdownRenderer>>
@@ -1735,15 +1748,11 @@ function ChatMarkdown({
1748 ? "breaks"
1749 : "plain"
1750 ];
1738 - // Extra and incremental plugins are per instance, so their output is not shared.
1739 - const uncachedPlugins = extraRemarkPlugins.length > 0 || incrementalParsing;
1740 - const remarkPlugins = uncachedPlugins
1741 - ? [
1742 - ...sharedRemarkPlugins,
1743 - ...extraRemarkPlugins,
1744 - ...(incrementalParsing ? [createIncrementalMarkdownPlugin()] : []),
1745 - ]
1746 - : sharedRemarkPlugins;
1751 + // The incremental parser keeps per-instance state, so its output is not shared.
1752 + const uncachedPlugins = incrementalParsing;
1753 + const remarkPlugins = incrementalParsing
1754 + ? [...sharedRemarkPlugins, ...extraRemarkPlugins, createIncrementalMarkdownPlugin()]
1755 + : withExtraPlugins(sharedRemarkPlugins, extraRemarkPlugins);
1756 const blocks = streamingBlocks ?? [text];
1757
1758 return (
apps/web/src/components/chat/TimelineWorkEntryRow.logic.ts
+4
@@ -1,3 +1,4 @@
1 +import type { PluggableList } from "unified";
2 import type { Root, RootContent } from "mdast";
3 import {
4 resolveWorkEntryToolPresentation,
@@ -237,3 +238,6 @@ export function remarkThoughtPreview(fallback: string) {
238 : [{ type: "text", value: fallback }];
239 };
240 }
241 +
242 +export const THOUGHT_PREVIEW_PLUGINS: PluggableList = [[remarkThoughtPreview, "Thought"]];
243 +export const THINKING_PREVIEW_PLUGINS: PluggableList = [[remarkThoughtPreview, "Thinking"]];
apps/web/src/components/chat/TimelineWorkEntryRow.tsx
+7 -7
@@ -64,7 +64,8 @@ import {
64 workEntryRawCommand,
65 buildToolCallExpandedBody,
66 workEntryIconName,
67 - remarkThoughtPreview,
67 + THINKING_PREVIEW_PLUGINS,
68 + THOUGHT_PREVIEW_PLUGINS,
69 } from "./TimelineWorkEntryRow.logic";
70
71 export function useThreadReadTarget(entry: TimelineWorkEntry, environmentId: EnvironmentId) {
@@ -421,12 +422,11 @@ function WorkEntryLogRow(props: WorkEntryRowProps) {
422 text={workEntry.detail ?? previewText}
423 cwd={ctx.markdownCwd}
424 parseRawHtml={false}
424 - extraRemarkPlugins={[
425 - [
426 - remarkThoughtPreview,
427 - workEntry.toolLifecycleStatus === "inProgress" ? "Thinking" : "Thought",
428 - ],
429 - ]}
425 + extraRemarkPlugins={
426 + workEntry.toolLifecycleStatus === "inProgress"
427 + ? THINKING_PREVIEW_PLUGINS
428 + : THOUGHT_PREVIEW_PLUGINS
429 + }
430 />
431 ) : threadLabel ? (
432 <ThreadReadLabel
apps/web/src/components/chat/TimelineWorkGroupRows.tsx
+4 -3
@@ -48,7 +48,8 @@ import {
48 SimpleWorkEntryRow,
49 } from "./TimelineWorkEntryRow";
50 import {
51 - remarkThoughtPreview,
51 + THINKING_PREVIEW_PLUGINS,
52 + THOUGHT_PREVIEW_PLUGINS,
53 toolGroupSummaryIconName,
54 workEntryIconName,
55 } from "./TimelineWorkEntryRow.logic";
@@ -537,7 +538,7 @@ export function LiveWorkEntryTimelineRow({
538 text={thoughtLine}
539 cwd={ctx.markdownCwd}
540 parseRawHtml={false}
540 - extraRemarkPlugins={[[remarkThoughtPreview, "Thought"]]}
541 + extraRemarkPlugins={THOUGHT_PREVIEW_PLUGINS}
542 />
543 </span>
544 }
@@ -558,7 +559,7 @@ export function LiveWorkEntryTimelineRow({
559 text={row.entry.detail ?? label}
560 cwd={ctx.markdownCwd}
561 parseRawHtml={false}
561 - extraRemarkPlugins={[[remarkThoughtPreview, row.active ? "Thinking" : "Thought"]]}
562 + extraRemarkPlugins={row.active ? THINKING_PREVIEW_PLUGINS : THOUGHT_PREVIEW_PLUGINS}
563 />
564 ) : threadLabel ? (
565 <ThreadReadLabel
apps/web/src/components/pullRequest/PullRequestDetailPanel.tsx
+2 -2
@@ -1,6 +1,5 @@
1 import { projectChangeRequestUrl } from "@t3tools/client-runtime/change-request-links";
2 import { normalizeThreadPullRequestKey } from "@t3tools/client-runtime/thread-pull-requests";
3 -import { changeRequestReferenceAutolinker } from "@t3tools/client-runtime/pull-request-markdown";
3 import { useAtomValue } from "@effect/atom-react";
4 import { usePullRequestStack } from "~/state/usePullRequestStack";
5 import {
@@ -106,6 +105,7 @@ import { PullRequestsUnavailableState } from "./PullRequestsUnavailableState";
105 import type { PullRequestAgentSelectionInput } from "./PullRequestCodeTab";
106 import { openOnHostLabel } from "./pullRequestLinkContextMenu";
107 import { PullRequestMarkdownContext } from "./PullRequestMarkdown";
108 +import { pullRequestAutolinkPlugins } from "./pullRequestMarkdown.logic";
109 import { PullRequestComposer } from "./PullRequestComposer";
110 import { PullRequestSummaryTab } from "./PullRequestSummaryTab";
111 import { PullRequestTimelineTab } from "./PullRequestTimelineTab";
@@ -240,7 +240,7 @@ function pullRequestMarkdownContext(
240 threadRef: ScopedThreadRef | null,
241 ) {
242 return {
243 - autolinks: changeRequestReferenceAutolinker(detail),
243 + remarkPlugins: pullRequestAutolinkPlugins(detail),
244 lineBreaks: sourceControlClients.find(detail?.provider ?? "")?.hardLineBreaks ?? false,
245 threadRef,
246 };
apps/web/src/components/pullRequest/PullRequestMarkdown.browser.test.tsx
+1 -1
@@ -7,7 +7,7 @@ import { PullRequestMarkdown, PullRequestMarkdownContext } from "./PullRequestMa
7
8 async function renderBody(host: string, options: { lineBreaks?: boolean } = {}) {
9 const context = {
10 - autolinks: null,
10 + remarkPlugins: [],
11 lineBreaks: sourceControlClients.get(host).hardLineBreaks ?? false,
12 threadRef: null,
13 };
apps/web/src/components/pullRequest/PullRequestMarkdown.tsx
+6 -12
@@ -1,9 +1,6 @@
1 import { ExternalLinkIcon, PaperclipIcon } from "lucide-react";
2 import { markdownImageSourceFragment } from "@t3tools/client-runtime/markdown-images";
3 -import {
4 - sourceControlClients,
5 - type ReferenceAutolink,
6 -} from "@t3tools/client-runtime/source-control-clients";
3 +import { sourceControlClients } from "@t3tools/client-runtime/source-control-clients";
4 import {
5 SOURCE_CONTROL_MEDIA_ASSET,
6 type AssetResource,
@@ -11,7 +8,7 @@ import {
8 type ScopedThreadRef,
9 } from "@t3tools/contracts";
10 import { createContext, useContext } from "react";
14 -import type { Options as ReactMarkdownOptions } from "react-markdown";
11 +import type { PluggableList } from "unified";
12
13 import { useAssetUrlRefresh, useAssetUrlState } from "~/assets/assetUrls";
14 import { cn } from "~/lib/utils";
@@ -19,10 +16,11 @@ import { PULL_REQUESTS_PANEL_REF } from "~/rightPanelStore";
16
17 import ChatMarkdown from "../ChatMarkdown";
18 import { MediaVideoPlayer } from "../media/MediaVideoPlayer";
22 -import { remarkPullRequestAutolinks, splitPullRequestBody } from "./pullRequestMarkdown.logic";
19 +import { splitPullRequestBody } from "./pullRequestMarkdown.logic";
20
21 export const PullRequestMarkdownContext = createContext<{
25 - autolinks: ((text: string) => ReadonlyArray<ReferenceAutolink>) | null;
22 + /** Stable per repository, so comments share one renderer and its parse cache. */
23 + remarkPlugins: PluggableList;
24 lineBreaks: boolean;
25 threadRef: ScopedThreadRef | null;
26 } | null>(null);
@@ -72,11 +70,7 @@ export function PullRequestMarkdown({
70 }) {
71 const segments = splitPullRequestBody(text);
72 const context = useContext(PullRequestMarkdownContext);
75 - const autolinks = context?.autolinks;
73 const resolvedThreadRef = threadRef ?? context?.threadRef ?? undefined;
77 - const extraRemarkPlugins: NonNullable<ReactMarkdownOptions["remarkPlugins"]> = autolinks
78 - ? [[remarkPullRequestAutolinks, { autolinks }]]
79 - : [];
74 return (
75 <div
76 className={cn(
@@ -95,7 +89,7 @@ export function PullRequestMarkdown({
89 threadRef={resolvedThreadRef}
90 pullRequestPanelRef={resolvedThreadRef ?? PULL_REQUESTS_PANEL_REF}
91 environmentId={environmentId}
98 - extraRemarkPlugins={extraRemarkPlugins}
92 + {...(context ? { extraRemarkPlugins: context.remarkPlugins } : {})}
93 lineBreaks={lineBreaks ?? context?.lineBreaks ?? false}
94 sourceControlMedia
95 />
apps/web/src/components/pullRequest/pullRequestMarkdown.logic.ts
+30 -1
@@ -1,4 +1,9 @@
1 -import type { ReferenceAutolink } from "@t3tools/client-runtime/source-control-clients";
1 +import { changeRequestReferenceAutolinker } from "@t3tools/client-runtime/pull-request-markdown";
2 +import {
3 + sourceControlClients,
4 + type ReferenceAutolink,
5 +} from "@t3tools/client-runtime/source-control-clients";
6 +import type { PluggableList } from "unified";
7
8 export {
9 splitPullRequestBody,
@@ -52,3 +57,27 @@ export function remarkPullRequestAutolinks(options: {
57 };
58 return (tree: MarkdownNode) => visit(tree);
59 }
60 +
61 +const NO_AUTOLINK_PLUGINS: PluggableList = [];
62 +const autolinkPlugins = new Map<string, PluggableList>();
63 +
64 +/**
65 + * One plugin list per repository, so every comment of a pull request shares one markdown
66 + * renderer and its parse cache instead of parsing again on each render.
67 + */
68 +export function pullRequestAutolinkPlugins(
69 + changeRequest: { readonly provider: string; readonly url: string } | null,
70 +): PluggableList {
71 + if (changeRequest === null) return NO_AUTOLINK_PLUGINS;
72 + const repositoryUrl = sourceControlClients.parseChangeRequestUrl(changeRequest.url)?.link
73 + .repositoryUrl;
74 + if (repositoryUrl === undefined) return NO_AUTOLINK_PLUGINS;
75 + const key = `${changeRequest.provider}\u0000${repositoryUrl}`;
76 + let plugins = autolinkPlugins.get(key);
77 + if (plugins === undefined) {
78 + const autolinks = changeRequestReferenceAutolinker(changeRequest);
79 + plugins = autolinks ? [[remarkPullRequestAutolinks, { autolinks }]] : NO_AUTOLINK_PLUGINS;
80 + autolinkPlugins.set(key, plugins);
81 + }
82 + return plugins;
83 +}