From 5bf11eac5517894c0b04e6f89eca6550f7718a76 Mon Sep 17 00:00:00 2001 From: Cursor Agent Date: Thu, 7 May 2026 17:50:44 +0000 Subject: [PATCH] Reduce timeline active-turn rerenders Co-authored-by: Julius Marminge --- .../chat/MessagesTimeline.logic.test.ts | 75 +++++++++++++++++++ .../components/chat/MessagesTimeline.logic.ts | 11 ++- .../src/components/chat/MessagesTimeline.tsx | 17 ++--- 3 files changed, 91 insertions(+), 12 deletions(-) diff --git a/apps/web/src/components/chat/MessagesTimeline.logic.test.ts b/apps/web/src/components/chat/MessagesTimeline.logic.test.ts index 633fb5d6be..2a79e457e7 100644 --- a/apps/web/src/components/chat/MessagesTimeline.logic.test.ts +++ b/apps/web/src/components/chat/MessagesTimeline.logic.test.ts @@ -252,6 +252,8 @@ describe("deriveMessagesTimelineRows", () => { ], completionDividerBeforeEntryId: "assistant-final-entry", isWorking: false, + activeTurnInProgress: false, + activeTurnId: null, activeTurnStartedAt: null, turnDiffSummaryByAssistantMessageId: new Map(), revertTurnCountByUserMessageId: new Map(), @@ -309,6 +311,8 @@ describe("deriveMessagesTimelineRows", () => { ], completionDividerBeforeEntryId: null, isWorking: false, + activeTurnInProgress: false, + activeTurnId: null, activeTurnStartedAt: null, turnDiffSummaryByAssistantMessageId: new Map([ ["assistant-1" as never, assistantTurnDiffSummary], @@ -366,6 +370,8 @@ describe("computeStableMessagesTimelineRows", () => { ], completionDividerBeforeEntryId: null, isWorking: false, + activeTurnInProgress: false, + activeTurnId: null, activeTurnStartedAt: null, turnDiffSummaryByAssistantMessageId: new Map(), revertTurnCountByUserMessageId: new Map(), @@ -382,6 +388,71 @@ describe("computeStableMessagesTimelineRows", () => { expect(repeated.result).toBe(initial.result); }); + it("keeps inactive assistant rows stable when the active turn changes", () => { + const firstAssistantMessage = { + id: "assistant-1" as never, + role: "assistant" as const, + text: "First", + turnId: "turn-1" as never, + createdAt: "2026-01-01T00:00:00Z", + completedAt: "2026-01-01T00:00:01Z", + streaming: false, + }; + const secondAssistantMessage = { + id: "assistant-2" as never, + role: "assistant" as const, + text: "Second", + turnId: "turn-2" as never, + createdAt: "2026-01-01T00:00:10Z", + completedAt: "2026-01-01T00:00:11Z", + streaming: false, + }; + const timelineEntries = [ + { + id: "entry-assistant-1", + kind: "message" as const, + createdAt: firstAssistantMessage.createdAt, + message: firstAssistantMessage, + }, + { + id: "entry-assistant-2", + kind: "message" as const, + createdAt: secondAssistantMessage.createdAt, + message: secondAssistantMessage, + }, + ]; + + const inactiveRows = deriveMessagesTimelineRows({ + timelineEntries, + completionDividerBeforeEntryId: null, + isWorking: false, + activeTurnInProgress: false, + activeTurnId: null, + activeTurnStartedAt: null, + turnDiffSummaryByAssistantMessageId: new Map(), + revertTurnCountByUserMessageId: new Map(), + }); + const initial = computeStableMessagesTimelineRows(inactiveRows, { + byId: new Map(), + result: [], + }); + + const activeRows = deriveMessagesTimelineRows({ + timelineEntries, + completionDividerBeforeEntryId: null, + isWorking: false, + activeTurnInProgress: true, + activeTurnId: "turn-2" as never, + activeTurnStartedAt: null, + turnDiffSummaryByAssistantMessageId: new Map(), + revertTurnCountByUserMessageId: new Map(), + }); + const repeated = computeStableMessagesTimelineRows(activeRows, initial); + + expect(repeated.result[0]).toBe(initial.result[0]); + expect(repeated.result[1]).not.toBe(initial.result[1]); + }); + it("reuses work rows when equivalent timeline derivations create new grouped arrays", () => { const firstWorkEntry = { id: "work-1", @@ -416,6 +487,8 @@ describe("computeStableMessagesTimelineRows", () => { ], completionDividerBeforeEntryId: null, isWorking: false, + activeTurnInProgress: false, + activeTurnId: null, activeTurnStartedAt: null, turnDiffSummaryByAssistantMessageId: new Map(), revertTurnCountByUserMessageId: new Map(), @@ -471,6 +544,8 @@ describe("computeStableMessagesTimelineRows", () => { ], completionDividerBeforeEntryId: null, isWorking: false, + activeTurnInProgress: false, + activeTurnId: null, activeTurnStartedAt: null, turnDiffSummaryByAssistantMessageId: new Map(), revertTurnCountByUserMessageId: new Map(), diff --git a/apps/web/src/components/chat/MessagesTimeline.logic.ts b/apps/web/src/components/chat/MessagesTimeline.logic.ts index c99f0b98a6..5256aaa13e 100644 --- a/apps/web/src/components/chat/MessagesTimeline.logic.ts +++ b/apps/web/src/components/chat/MessagesTimeline.logic.ts @@ -1,7 +1,7 @@ import * as Equal from "effect/Equal"; import { type TimelineEntry, type WorkLogEntry } from "../../session-logic"; import { type ChatMessage, type ProposedPlan, type TurnDiffSummary } from "../../types"; -import { type MessageId } from "@t3tools/contracts"; +import { type MessageId, type TurnId } from "@t3tools/contracts"; export const MAX_VISIBLE_WORK_LOG_ENTRIES = 6; @@ -27,6 +27,7 @@ export type MessagesTimelineRow = durationStart: string; showCompletionDivider: boolean; showAssistantCopyButton: boolean; + assistantTurnStillInProgress: boolean; assistantTurnDiffSummary?: TurnDiffSummary | undefined; revertTurnCount?: number | undefined; } @@ -112,6 +113,8 @@ export function deriveMessagesTimelineRows(input: { timelineEntries: ReadonlyArray; completionDividerBeforeEntryId: string | null; isWorking: boolean; + activeTurnInProgress: boolean; + activeTurnId: TurnId | null | undefined; activeTurnStartedAt: string | null; turnDiffSummaryByAssistantMessageId: ReadonlyMap; revertTurnCountByUserMessageId: ReadonlyMap; @@ -170,6 +173,11 @@ export function deriveMessagesTimelineRows(input: { showAssistantCopyButton: timelineEntry.message.role === "assistant" && terminalAssistantMessageIds.has(timelineEntry.message.id), + assistantTurnStillInProgress: + timelineEntry.message.role === "assistant" && + input.activeTurnInProgress && + input.activeTurnId != null && + timelineEntry.message.turnId === input.activeTurnId, assistantTurnDiffSummary: timelineEntry.message.role === "assistant" ? input.turnDiffSummaryByAssistantMessageId.get(timelineEntry.message.id) @@ -233,6 +241,7 @@ function isRowUnchanged(a: MessagesTimelineRow, b: MessagesTimelineRow): boolean a.durationStart === bm.durationStart && a.showCompletionDivider === bm.showCompletionDivider && a.showAssistantCopyButton === bm.showAssistantCopyButton && + a.assistantTurnStillInProgress === bm.assistantTurnStillInProgress && a.assistantTurnDiffSummary === bm.assistantTurnDiffSummary && a.revertTurnCount === bm.revertTurnCount ); diff --git a/apps/web/src/components/chat/MessagesTimeline.tsx b/apps/web/src/components/chat/MessagesTimeline.tsx index dafa8161bd..c44dabe68b 100644 --- a/apps/web/src/components/chat/MessagesTimeline.tsx +++ b/apps/web/src/components/chat/MessagesTimeline.tsx @@ -82,8 +82,6 @@ interface TimelineRowSharedState { } interface TimelineRowActivityState { - activeTurnInProgress: boolean; - activeTurnId: TurnId | null; isWorking: boolean; isRevertingCheckpoint: boolean; completionSummary: string | null; @@ -155,6 +153,8 @@ export const MessagesTimeline = memo(function MessagesTimeline({ timelineEntries, completionDividerBeforeEntryId, isWorking, + activeTurnInProgress, + activeTurnId, activeTurnStartedAt, turnDiffSummaryByAssistantMessageId, revertTurnCountByUserMessageId, @@ -163,6 +163,8 @@ export const MessagesTimeline = memo(function MessagesTimeline({ timelineEntries, completionDividerBeforeEntryId, isWorking, + activeTurnInProgress, + activeTurnId, activeTurnStartedAt, turnDiffSummaryByAssistantMessageId, revertTurnCountByUserMessageId, @@ -221,13 +223,11 @@ export const MessagesTimeline = memo(function MessagesTimeline({ ); const activityState = useMemo( () => ({ - activeTurnInProgress, - activeTurnId: activeTurnId ?? null, isWorking, isRevertingCheckpoint, completionSummary, }), - [activeTurnInProgress, activeTurnId, completionSummary, isRevertingCheckpoint, isWorking], + [completionSummary, isRevertingCheckpoint, isWorking], ); // Stable renderItem — no closure deps. Row components read shared state @@ -450,15 +450,10 @@ function AssistantCompletionDivider() { } function AssistantCopyButton({ row }: { row: Extract }) { - const activity = use(TimelineRowActivityCtx); - const assistantTurnStillInProgress = - activity.activeTurnInProgress && - activity.activeTurnId !== null && - row.message.turnId === activity.activeTurnId; const assistantCopyState = resolveAssistantMessageCopyState({ text: row.message.text ?? null, showCopyButton: row.showAssistantCopyButton, - streaming: row.message.streaming || assistantTurnStillInProgress, + streaming: row.message.streaming || row.assistantTurnStillInProgress, }); if (!assistantCopyState.visible) {