TypeScript
fix(web): streaming replies no longer re-render every visible timeline row (#27420)
Julius Marminge5cbc207d8c8e
diff --git a/apps/web/src/components/chat/MessagesTimeline.tsx b/apps/web/src/components/chat/MessagesTimeline.tsxindex a9cab1554a3..faa733c76e8 100644--- a/apps/web/src/components/chat/MessagesTimeline.tsx+++ b/apps/web/src/components/chat/MessagesTimeline.tsx@@ -659,12 +659,11 @@ const ConversationTimeline = memo(function ConversationTimeline({? new Set(paintedExpandedAttemptIds).add(activeFindAttemptId): paintedExpandedAttemptIds;- const pendingApprovalsByRequestId = new Map(- pendingApprovals- .filter((approval) => !heldApprovalRequestIds?.has(approval.requestId))- .map((approval) => [approval.requestId, approval]),+ const isVisiblePendingApproval = (approval: PendingApproval) =>+ !heldApprovalRequestIds?.has(approval.requestId);+ const pendingApprovalRequestIds = new Set(+ pendingApprovals.filter(isVisiblePendingApproval).map((approval) => approval.requestId),);- const pendingApprovalRequestIds = new Set(pendingApprovalsByRequestId.keys());const rawRows = useIncrementalTimelineRows(`${listIdentityKey}\0${workspaceRoot ?? ""}`, {timelineEntries,pendingApprovalRequestIds,@@ -683,17 +682,19 @@ const ConversationTimeline = memo(function ConversationTimeline({worktreeSetup,});const rows = useStableRows(rawRows, listIdentityKey);- const unmatchedPendingApprovals = new Map(pendingApprovalsByRequestId);- if (unmatchedPendingApprovals.size > 0) {- for (const row of rows) {- const entries =- row.kind === "work" ? row.groupedEntries : row.kind === "work-live" ? [row.entry] : [];- for (const entry of entries) {- const item = entry.projectedItem?.item;- if (item?.type === "approval_request") unmatchedPendingApprovals.delete(item.requestId);- }+ const matchedApprovalRequestIds = new Set<string>();+ for (const row of pendingApprovalRequestIds.size > 0 ? rows : []) {+ const entries =+ row.kind === "work" ? row.groupedEntries : row.kind === "work-live" ? [row.entry] : [];+ for (const entry of entries) {+ const item = entry.projectedItem?.item;+ if (item?.type === "approval_request") matchedApprovalRequestIds.add(item.requestId);}}+ const unmatchedPendingApprovals = pendingApprovals.filter(+ (approval) =>+ isVisiblePendingApproval(approval) && !matchedApprovalRequestIds.has(approval.requestId),+ );// Run status/timestamps change per stream event; keep them out of shared row context.const runs = useStableHandoffRuns(runsProp);const minimapItems = deriveTimelineMinimapItems(rows);@@ -706,7 +707,7 @@ const ConversationTimeline = memo(function ConversationTimeline({restoringThreadPosition && restoreRowIndex >= 0 ? { indices: [restoreRowIndex] } : undefined;const showsEmptyState =rows.length === 0 &&- unmatchedPendingApprovals.size === 0 &&+ unmatchedPendingApprovals.length === 0 &&!isWorking &&parentThreadLink === null &&historyControls === undefined &&@@ -1000,6 +1001,13 @@ const ConversationTimeline = memo(function ConversationTimeline({};};+ // Built from props after the last hook so the compiler can memoize the row context; a map+ // made before a hook would rebuild it, and re-render every row, on each streamed update.+ const pendingApprovalsByRequestId = new Map(+ pendingApprovals+ .filter(isVisiblePendingApproval)+ .map((approval) => [approval.requestId, approval]),+ );const sharedState: TimelineRowSharedState = {pendingApprovals: pendingApprovalsByRequestId,respondingRequestIds,@@ -1238,12 +1246,12 @@ const ConversationTimeline = memo(function ConversationTimeline({onGrow={onFooterGrow}>{footer}- {unmatchedPendingApprovals.size > 0 ? (+ {unmatchedPendingApprovals.length > 0 ? (<TimelineRowCtx value={sharedState}><div className="messages-timeline-row-frame"><div className="chat-content-lane overflow-x-clip" data-timeline-root="true"><WorkLogBlock>- {[...unmatchedPendingApprovals.values()].map((approval) => (+ {unmatchedPendingApprovals.map((approval) => (<TimelinePendingApprovalRow key={approval.requestId} approval={approval} />))}</WorkLogBlock>diff --git a/apps/web/test/appScenarios.renders.tsx b/apps/web/test/appScenarios.renders.tsxindex 9c1a7d520ee..cc7d67798dd 100644--- a/apps/web/test/appScenarios.renders.tsx+++ b/apps/web/test/appScenarios.renders.tsx@@ -201,6 +201,9 @@ it("app-stream-assistant-turn", async () => {.element(page.getByText(`word${STREAM_DELTAS - 1}`, { exact: false })).toBeVisible();});+ // Only the streaming row changes; history rows must not re-render with it.+ expect(counted.components.UserTimelineRow).toBeUndefined();+ expect(counted.components.AssistantTimelineRow).toBe(STREAM_DELTAS);await report("app-stream-assistant-turn", [counted], { commits: false });});