Skip to content

Commit 0f21bd2

Browse files
Yuruzuuclaude
andcommitted
perf(web): chat view redraws no longer re-render the composer or side chat timeline
Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent 37d7615 commit 0f21bd2

5 files changed

Lines changed: 86 additions & 29 deletions

File tree

‎apps/web/src/components/ChatView.tsx‎

Lines changed: 17 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -381,6 +381,7 @@ const EMPTY_FEEDBACK_SUBMISSIONS: ReadonlyArray<CodexFeedbackSubmission> = [];
381381
const EMPTY_PROVIDER_SKILLS: ServerProvider["skills"] = [];
382382
const EMPTY_TURN_ITEMS: ThreadStructure["turnItems"] = [];
383383
const EMPTY_AUTO_UPDATE_ENVIRONMENTS: ReadonlyArray<EnvironmentPresentation> = [];
384+
const EMPTY_COMPOSER_BANNER_ITEMS: ReadonlyArray<ComposerBannerStackItem> = [];
384385

385386
const EMPTY_PENDING_FILE_SURFACE_IDS: ReadonlySet<string> = new Set();
386387

@@ -865,20 +866,18 @@ export default function ChatView(props: ChatViewProps) {
865866
const sideChatParent = useQuietThreadShell(sideChatParentRef);
866867
const parentThreadLink = (() => {
867868
if (sideChatParentRef !== null) {
869+
const title = sideChatParent?.title ?? "Parent thread";
870+
// Embedded, the parent is the view around it.
871+
if (embedded) return { title, sideChat: true };
868872
return {
869-
title: sideChatParent?.title ?? "Parent thread",
873+
title,
870874
sideChat: true,
871-
// Embedded, the parent is the view around it.
872-
...(embedded
873-
? {}
874-
: {
875-
open: (): void => {
876-
void navigate({
877-
to: "/$environmentId/$threadId",
878-
params: buildThreadRouteParams(sideChatParentRef),
879-
});
880-
},
881-
}),
875+
open: (): void => {
876+
void navigate({
877+
to: "/$environmentId/$threadId",
878+
params: buildThreadRouteParams(sideChatParentRef),
879+
});
880+
},
882881
};
883882
}
884883
if (parentSubagentThreadRef !== null) {
@@ -3584,7 +3583,7 @@ export default function ChatView(props: ChatViewProps) {
35843583
activeThreadId={activeThreadId}
35853584
activeThread={activeThread}
35863585
activeThreadShell={activeThreadShell}
3587-
promptHistoryMessages={timelineMessages}
3586+
promptHistoryEntries={timelineEntries}
35883587
forceExpandedOnMobile={
35893588
forceExpandedMobileComposer && isDraftHeroState
35903589
}
@@ -3619,7 +3618,11 @@ export default function ChatView(props: ChatViewProps) {
36193618
: projectCloneSendBlockReason
36203619
}
36213620
isPreparingWorktree={isPreparingWorktree && !sendQueuesBehindSetup}
3622-
bannerItems={isDraftHeroState ? [] : composerBannerItems}
3621+
bannerItems={
3622+
isDraftHeroState || composerBannerItems.length === 0
3623+
? EMPTY_COMPOSER_BANNER_ITEMS
3624+
: composerBannerItems
3625+
}
36233626
resumeCompactionTokens={resumeCompactionTokens}
36243627
keepFullHistory={keepFullHistory}
36253628
onToggleKeepFullHistory={toggleKeepFullHistoryStable}

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

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -235,19 +235,14 @@ import { proposedPlanTitle } from "@t3tools/shared/proposedPlanText";
235235
import { hasProviderSetup } from "./ProviderStatusBanner";
236236
import { type AppModelOption, getAppModelOptionsForInstance } from "../../modelSelection";
237237
import type { UnifiedSettings } from "@t3tools/contracts/settings";
238-
import {
239-
isVideoAttachment,
240-
type ChatMessage,
241-
type SessionPhase,
242-
type Thread,
243-
type ThreadShell,
244-
} from "../../types";
238+
import { isVideoAttachment, type SessionPhase, type Thread, type ThreadShell } from "../../types";
245239
import { type ComposerPromptHistoryPosition } from "./composerPromptHistory";
246240
import type { PendingUserInputDraftAnswer } from "@t3tools/client-runtime/state/pending-user-input";
247241
import type {
248242
LatestProposedPlanState,
249243
PendingApproval,
250244
PendingUserInput,
245+
TimelineEntry,
251246
} from "../../session-logic";
252247
import {
253248
resolveComposerDispatchMode,
@@ -367,7 +362,7 @@ export interface ChatComposerProps {
367362
activeThreadId: ThreadId | null;
368363
activeThread: Thread | undefined;
369364
activeThreadShell: ThreadShell | null;
370-
promptHistoryMessages: ReadonlyArray<ChatMessage>;
365+
promptHistoryEntries: ReadonlyArray<TimelineEntry>;
371366
forceExpandedOnMobile: boolean;
372367
projectSelectionRequired: boolean;
373368

@@ -520,7 +515,7 @@ export const ChatComposer = memo(function ChatComposer(props: ChatComposerProps)
520515
onMultipleModelSelectionsChange: setMultipleModelSelections,
521516
activeThreadId,
522517
activeThread,
523-
promptHistoryMessages,
518+
promptHistoryEntries,
524519
forceExpandedOnMobile,
525520
projectSelectionRequired,
526521
phase,
@@ -2194,7 +2189,7 @@ export const ChatComposer = memo(function ChatComposer(props: ChatComposerProps)
21942189
planModeUiEnabled,
21952190
isLiteralPendingAnswer,
21962191
pendingUserInputs,
2197-
promptHistoryMessages,
2192+
promptHistoryEntries,
21982193
promptHistoryPositionRef,
21992194
composerDraftTarget,
22002195
promptRef,

‎apps/web/src/components/chat/composerCommandKey.ts‎

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ export function composerCommandKeyHandler(input: {
4444
readonly planModeUiEnabled: boolean;
4545
readonly isLiteralPendingAnswer: boolean;
4646
readonly pendingUserInputs: ReadonlyArray<PendingUserInput>;
47-
readonly promptHistoryMessages: ChatComposerProps["promptHistoryMessages"];
47+
readonly promptHistoryEntries: ChatComposerProps["promptHistoryEntries"];
4848
readonly promptHistoryPositionRef: RefObject<{
4949
targetKey: string;
5050
position: ComposerPromptHistoryPosition;
@@ -96,7 +96,7 @@ export function composerCommandKeyHandler(input: {
9696
planModeUiEnabled,
9797
isLiteralPendingAnswer,
9898
pendingUserInputs,
99-
promptHistoryMessages,
99+
promptHistoryEntries,
100100
promptHistoryPositionRef,
101101
composerDraftTarget,
102102
promptRef,
@@ -165,7 +165,9 @@ export function composerCommandKeyHandler(input: {
165165
const step = stepComposerPromptHistory({
166166
direction,
167167
// Built on the keypress, not per render: the timeline changes on every streamed delta.
168-
entries: buildComposerPromptHistoryEntries(promptHistoryMessages),
168+
entries: buildComposerPromptHistoryEntries(
169+
promptHistoryEntries.flatMap((entry) => (entry.kind === "message" ? [entry.message] : [])),
170+
),
169171
position,
170172
currentPrompt: promptRef.current,
171173
});

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

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
// chat and settings routes, the sidebar and the right panel, through the same stores and
44
// atoms the app reads. See renderScenarios.renders.tsx for the counting rules.
55
import { createMemoryHistory } from "@tanstack/react-router";
6+
import { scopeThreadRef } from "@t3tools/client-runtime/environment";
67
import type { ThreadId } from "@t3tools/contracts";
78
import { DEFAULT_CLIENT_SETTINGS } from "@t3tools/contracts/settings";
89
import type * as Context from "effect/Context";
@@ -13,11 +14,13 @@ import "~/index.css";
1314

1415
import { AppRoot } from "~/AppRoot";
1516
import { __setClientSettingsForTests } from "~/hooks/useSettings";
17+
import { useRightPanelStore } from "~/rightPanelStore";
1618
import { getRouter, type AppRouter } from "~/router";
1719
import {
1820
ENVIRONMENT_ID,
1921
assistantItem,
2022
emitThreadEvent,
23+
emitVcsStatus,
2124
installFakeEnvironment,
2225
isThreadSubscribed,
2326
runningRun,
@@ -47,6 +50,7 @@ vi.mock(import("~/rpc/requestLatencyState"), async (original) => ({
4750
trackRpcRequestSent: () => {},
4851
}));
4952

53+
const GIT_STATUS_UPDATES = 20;
5054
const HISTORY_TURNS = 20;
5155
const STREAM_DELTAS = 30;
5256
const TYPED_TEXT = "fix the flaky tests!";
@@ -61,7 +65,9 @@ const [WARM, A, B, LIVE] = THREADS.map((thread) => thread.id) as [
6165
ThreadId,
6266
];
6367

64-
installFakeEnvironment(THREADS);
68+
const SIDE_CHAT = { ...threadWithHistory("thread-side", HISTORY_TURNS), sideChatOf: WARM };
69+
70+
installFakeEnvironment([...THREADS, SIDE_CHAT]);
6571
// Desktop-sized, so the sidebar, composer and right panel lay out as they do in the app.
6672
await page.viewport(1400, 900);
6773
// A returning user: the first-run gate would otherwise wait on onboarding evidence.
@@ -287,3 +293,33 @@ it("app-switch-settings-section", async () => {
287293
await measure(openSettings(router, "/settings/appearance")),
288294
]);
289295
});
296+
297+
// ChatView re-renders for any git status change; the composer and side chat timeline should not.
298+
it("app-git-status-updates-with-side-chat", async () => {
299+
using _clock = frozenClock();
300+
useRightPanelStore.getState().openSideChat(scopeThreadRef(ENVIRONMENT_ID, WARM), SIDE_CHAT.id);
301+
await using _app = (await openThread(WARM)).screen;
302+
await expect.element(lastAnswer(SIDE_CHAT.id)).toBeVisible();
303+
const local = (insertions: number) => ({
304+
isRepo: true,
305+
hasPrimaryRemote: true,
306+
isDefaultRef: true,
307+
refName: "main",
308+
hasWorkingTreeChanges: true,
309+
workingTree: {
310+
files: [{ path: "src/app.ts", insertions, deletions: 0 }],
311+
insertions,
312+
deletions: 0,
313+
},
314+
});
315+
await act(() => emitVcsStatus({ _tag: "snapshot", local: local(0), remote: null }));
316+
const counted = await measure(async () => {
317+
for (let update = 1; update <= GIT_STATUS_UPDATES; update++) {
318+
await act(() => emitVcsStatus({ _tag: "localUpdated", local: local(update) }));
319+
}
320+
});
321+
expect(counted.components.ChatView).toBe(GIT_STATUS_UPDATES * 2);
322+
expect(counted.components.ChatComposer).toBeUndefined();
323+
expect(counted.components.ConversationTimeline).toBeUndefined();
324+
await report("app-git-status-updates-with-side-chat", [counted]);
325+
});

‎apps/web/test/fakeEnvironment.ts‎

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import {
1717
TurnItemId,
1818
WS_METHODS,
1919
WsRpcGroup,
20+
type VcsStatusStreamEvent,
2021
type OrchestrationProjectShell,
2122
type OrchestrationV2ProjectedTurnItem,
2223
type OrchestrationV2Run,
@@ -181,6 +182,8 @@ function run(threadId: ThreadId, id: RunId, ordinal: number, userMessageId: stri
181182
}
182183

183184
export interface FakeThread {
185+
/** Makes this a side chat of that thread. */
186+
readonly sideChatOf?: ThreadId;
184187
readonly id: ThreadId;
185188
readonly title: string;
186189
readonly runs: ReadonlyArray<OrchestrationV2Run>;
@@ -223,7 +226,12 @@ function shellOf(thread: FakeThread): OrchestrationV2ThreadShell {
223226
branch: "main",
224227
worktreePath: null,
225228
activeProviderThreadId: null,
226-
lineage: { rootThreadId: thread.id, parentThreadId: null, relationshipToParent: null },
229+
lineage: {
230+
rootThreadId: thread.sideChatOf ?? thread.id,
231+
parentThreadId: thread.sideChatOf ?? null,
232+
relationshipToParent: null,
233+
...(thread.sideChatOf ? { sideChat: true } : {}),
234+
},
227235
forkedFrom: null,
228236
createdBy: "user",
229237
creationSource: "web",
@@ -309,6 +317,7 @@ const json = (body: unknown, status = 200) =>
309317
const threadSubscribers = new Map<string, Queue.Queue<OrchestrationV2ThreadStreamItem>>();
310318
let threads: ReadonlyArray<FakeThread> = [];
311319
let sequence = 1;
320+
let vcsStatusSubscriber: Queue.Queue<VcsStatusStreamEvent> | null = null;
312321

313322
const subscribe = <A>(first: ReadonlyArray<A>, register?: (queue: Queue.Queue<A>) => () => void) =>
314323
Stream.unwrap(
@@ -356,6 +365,13 @@ const handlers: Record<string, (input: never) => unknown> = {
356365
},
357366
);
358367
},
368+
[WS_METHODS.subscribeVcsStatus]: () =>
369+
subscribe<VcsStatusStreamEvent>([], (queue) => {
370+
vcsStatusSubscriber = queue;
371+
return () => {
372+
if (vcsStatusSubscriber === queue) vcsStatusSubscriber = null;
373+
};
374+
}),
359375
[WS_METHODS.subscribeServerConfig]: () =>
360376
subscribe([{ version: 1 as const, type: "snapshot" as const, config: serverConfig }]),
361377
};
@@ -468,3 +484,8 @@ export function runningRun(threadId: ThreadId, id: string, ordinal: number, user
468484
}
469485

470486
export { userItem };
487+
488+
export async function emitVcsStatus(event: VcsStatusStreamEvent): Promise<void> {
489+
if (vcsStatusSubscriber === null) throw new Error("No VCS status subscription");
490+
await Effect.runPromise(Queue.offer(vcsStatusSubscriber, event));
491+
}

0 commit comments

Comments
 (0)