Skip to content

Commit 5cbc207

Browse files
fix(web): streaming replies no longer re-render every visible timeline row (pingdotgg#27420)
Co-authored-by: Claude Opus 5.5 <[email protected]>
1 parent 305a90d commit 5cbc207

2 files changed

Lines changed: 28 additions & 17 deletions

File tree

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

Lines changed: 25 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -659,12 +659,11 @@ const ConversationTimeline = memo(function ConversationTimeline({
659659
? new Set(paintedExpandedAttemptIds).add(activeFindAttemptId)
660660
: paintedExpandedAttemptIds;
661661

662-
const pendingApprovalsByRequestId = new Map(
663-
pendingApprovals
664-
.filter((approval) => !heldApprovalRequestIds?.has(approval.requestId))
665-
.map((approval) => [approval.requestId, approval]),
662+
const isVisiblePendingApproval = (approval: PendingApproval) =>
663+
!heldApprovalRequestIds?.has(approval.requestId);
664+
const pendingApprovalRequestIds = new Set(
665+
pendingApprovals.filter(isVisiblePendingApproval).map((approval) => approval.requestId),
666666
);
667-
const pendingApprovalRequestIds = new Set(pendingApprovalsByRequestId.keys());
668667
const rawRows = useIncrementalTimelineRows(`${listIdentityKey}\0${workspaceRoot ?? ""}`, {
669668
timelineEntries,
670669
pendingApprovalRequestIds,
@@ -683,17 +682,19 @@ const ConversationTimeline = memo(function ConversationTimeline({
683682
worktreeSetup,
684683
});
685684
const rows = useStableRows(rawRows, listIdentityKey);
686-
const unmatchedPendingApprovals = new Map(pendingApprovalsByRequestId);
687-
if (unmatchedPendingApprovals.size > 0) {
688-
for (const row of rows) {
689-
const entries =
690-
row.kind === "work" ? row.groupedEntries : row.kind === "work-live" ? [row.entry] : [];
691-
for (const entry of entries) {
692-
const item = entry.projectedItem?.item;
693-
if (item?.type === "approval_request") unmatchedPendingApprovals.delete(item.requestId);
694-
}
685+
const matchedApprovalRequestIds = new Set<string>();
686+
for (const row of pendingApprovalRequestIds.size > 0 ? rows : []) {
687+
const entries =
688+
row.kind === "work" ? row.groupedEntries : row.kind === "work-live" ? [row.entry] : [];
689+
for (const entry of entries) {
690+
const item = entry.projectedItem?.item;
691+
if (item?.type === "approval_request") matchedApprovalRequestIds.add(item.requestId);
695692
}
696693
}
694+
const unmatchedPendingApprovals = pendingApprovals.filter(
695+
(approval) =>
696+
isVisiblePendingApproval(approval) && !matchedApprovalRequestIds.has(approval.requestId),
697+
);
697698
// Run status/timestamps change per stream event; keep them out of shared row context.
698699
const runs = useStableHandoffRuns(runsProp);
699700
const minimapItems = deriveTimelineMinimapItems(rows);
@@ -706,7 +707,7 @@ const ConversationTimeline = memo(function ConversationTimeline({
706707
restoringThreadPosition && restoreRowIndex >= 0 ? { indices: [restoreRowIndex] } : undefined;
707708
const showsEmptyState =
708709
rows.length === 0 &&
709-
unmatchedPendingApprovals.size === 0 &&
710+
unmatchedPendingApprovals.length === 0 &&
710711
!isWorking &&
711712
parentThreadLink === null &&
712713
historyControls === undefined &&
@@ -1000,6 +1001,13 @@ const ConversationTimeline = memo(function ConversationTimeline({
10001001
};
10011002
};
10021003

1004+
// Built from props after the last hook so the compiler can memoize the row context; a map
1005+
// made before a hook would rebuild it, and re-render every row, on each streamed update.
1006+
const pendingApprovalsByRequestId = new Map(
1007+
pendingApprovals
1008+
.filter(isVisiblePendingApproval)
1009+
.map((approval) => [approval.requestId, approval]),
1010+
);
10031011
const sharedState: TimelineRowSharedState = {
10041012
pendingApprovals: pendingApprovalsByRequestId,
10051013
respondingRequestIds,
@@ -1238,12 +1246,12 @@ const ConversationTimeline = memo(function ConversationTimeline({
12381246
onGrow={onFooterGrow}
12391247
>
12401248
{footer}
1241-
{unmatchedPendingApprovals.size > 0 ? (
1249+
{unmatchedPendingApprovals.length > 0 ? (
12421250
<TimelineRowCtx value={sharedState}>
12431251
<div className="messages-timeline-row-frame">
12441252
<div className="chat-content-lane overflow-x-clip" data-timeline-root="true">
12451253
<WorkLogBlock>
1246-
{[...unmatchedPendingApprovals.values()].map((approval) => (
1254+
{unmatchedPendingApprovals.map((approval) => (
12471255
<TimelinePendingApprovalRow key={approval.requestId} approval={approval} />
12481256
))}
12491257
</WorkLogBlock>

‎apps/web/test/appScenarios.renders.tsx‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -201,6 +201,9 @@ it("app-stream-assistant-turn", async () => {
201201
.element(page.getByText(`word${STREAM_DELTAS - 1}`, { exact: false }))
202202
.toBeVisible();
203203
});
204+
// Only the streaming row changes; history rows must not re-render with it.
205+
expect(counted.components.UserTimelineRow).toBeUndefined();
206+
expect(counted.components.AssistantTimelineRow).toBe(STREAM_DELTAS);
204207
await report("app-stream-assistant-turn", [counted], { commits: false });
205208
});
206209

0 commit comments

Comments
 (0)