Skip to content

Commit 211618f

Browse files
perf(web): huge-thread switch no longer blanks the chat pane (#11169)
Co-authored-by: Cursor Agent <[email protected]> Co-authored-by: Julius Marminge <[email protected]>
1 parent 8078c53 commit 211618f

4 files changed

Lines changed: 472 additions & 44 deletions

File tree

‎apps/web/src/components/ChatView.logic.test.ts‎

Lines changed: 181 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,14 @@ import {
5959
resolveSendEnvMode,
6060
threadShellHasStarted,
6161
resolveDraftHeroState,
62+
isPaintOnlyThreadTimeline,
63+
peekHeldThreadTimeline,
64+
peekRememberedThreadTimeline,
65+
rememberReadyThreadTimeline,
66+
resetHeldThreadTimeline,
67+
resolveThreadSwitchTimeline,
68+
threadKeysShareEnvironment,
69+
timelineHasEphemeralPreviewUrls,
6270
scheduleEnvironmentReconnectWarning,
6371
startNewThreadForProject,
6472
codexArtifactTemplatePromptToAppend,
@@ -565,6 +573,179 @@ describe("draft hero submission transition", () => {
565573
});
566574
});
567575

576+
describe("resolveThreadSwitchTimeline", () => {
577+
afterEach(() => {
578+
resetHeldThreadTimeline();
579+
});
580+
581+
const held = { threadKey: "env-1:thread-a", entries: ["a1", "a2"] };
582+
583+
it("keeps the previous thread's entries while the next thread is loading", () => {
584+
expect(
585+
resolveThreadSwitchTimeline({
586+
loading: true,
587+
activeThreadKey: "env-1:thread-b",
588+
nextEntries: [],
589+
lastReady: held,
590+
}),
591+
).toEqual({ entries: ["a1", "a2"], displayThreadKey: "env-1:thread-a" });
592+
});
593+
594+
it("shows the new thread once its detail is ready", () => {
595+
expect(
596+
resolveThreadSwitchTimeline({
597+
loading: false,
598+
activeThreadKey: "env-1:thread-b",
599+
nextEntries: ["b1"],
600+
lastReady: held,
601+
}),
602+
).toEqual({ entries: ["b1"], displayThreadKey: "env-1:thread-b" });
603+
});
604+
605+
it("does not invent a timeline on the first open of a thread", () => {
606+
expect(
607+
resolveThreadSwitchTimeline({
608+
loading: true,
609+
activeThreadKey: "env-1:thread-a",
610+
nextEntries: [],
611+
lastReady: null,
612+
}),
613+
).toEqual({ entries: [], displayThreadKey: "env-1:thread-a" });
614+
});
615+
616+
it("keeps the held thread workspace cwd with the snapshot", () => {
617+
rememberReadyThreadTimeline({
618+
...held,
619+
markdownCwd: "/repo/a",
620+
workspaceRoot: "/repo/a",
621+
});
622+
expect(peekHeldThreadTimeline<string[]>()).toEqual({
623+
...held,
624+
markdownCwd: "/repo/a",
625+
workspaceRoot: "/repo/a",
626+
});
627+
});
628+
629+
it("survives a ChatView remount by remembering the last ready timeline", () => {
630+
rememberReadyThreadTimeline(held);
631+
expect(peekHeldThreadTimeline<string[]>()).toEqual(held);
632+
expect(
633+
resolveThreadSwitchTimeline({
634+
loading: true,
635+
activeThreadKey: "env-1:thread-b",
636+
nextEntries: [],
637+
}),
638+
).toEqual({ entries: ["a1", "a2"], displayThreadKey: "env-1:thread-a" });
639+
});
640+
641+
it("paints a remembered destination instead of the last-viewed thread", () => {
642+
rememberReadyThreadTimeline(held);
643+
rememberReadyThreadTimeline({ threadKey: "env-1:thread-b", entries: ["b1", "b2"] });
644+
expect(peekRememberedThreadTimeline<string[]>("env-1:thread-a")).toEqual(["a1", "a2"]);
645+
expect(
646+
resolveThreadSwitchTimeline({
647+
loading: true,
648+
activeThreadKey: "env-1:thread-a",
649+
nextEntries: [],
650+
}),
651+
).toEqual({ entries: ["a1", "a2"], displayThreadKey: "env-1:thread-a" });
652+
});
653+
654+
it("prefers live entries over a remembered snapshot", () => {
655+
rememberReadyThreadTimeline({ threadKey: "env-1:thread-b", entries: ["stale-b"] });
656+
expect(
657+
resolveThreadSwitchTimeline({
658+
loading: false,
659+
activeThreadKey: "env-1:thread-b",
660+
nextEntries: ["fresh-b"],
661+
}),
662+
).toEqual({ entries: ["fresh-b"], displayThreadKey: "env-1:thread-b" });
663+
});
664+
665+
it("does not keep a remembered snapshot on a resolved empty thread", () => {
666+
rememberReadyThreadTimeline(held);
667+
expect(
668+
resolveThreadSwitchTimeline({
669+
loading: false,
670+
activeThreadKey: "env-1:thread-a",
671+
nextEntries: [],
672+
}),
673+
).toEqual({ entries: [], displayThreadKey: "env-1:thread-a" });
674+
});
675+
676+
it("does not hold another environment's timeline across a jump", () => {
677+
expect(threadKeysShareEnvironment("env-1:thread-a", "env-2:thread-b")).toBe(false);
678+
expect(
679+
resolveThreadSwitchTimeline({
680+
loading: true,
681+
activeThreadKey: "env-2:thread-b",
682+
nextEntries: [],
683+
lastReady: held,
684+
}),
685+
).toEqual({ entries: [], displayThreadKey: "env-2:thread-b" });
686+
});
687+
688+
it("treats a foreign held timeline as paint-only", () => {
689+
expect(isPaintOnlyThreadTimeline("env-1:thread-a", "env-1:thread-b")).toBe(true);
690+
expect(isPaintOnlyThreadTimeline("env-1:thread-b", "env-1:thread-b")).toBe(false);
691+
});
692+
693+
it("does not remember a timeline that still has handoff blob previews", () => {
694+
expect(
695+
timelineHasEphemeralPreviewUrls([
696+
{
697+
kind: "message",
698+
message: {
699+
id: MessageId.make("preview-message"),
700+
role: "user",
701+
text: "Preview",
702+
turnId: null,
703+
streaming: false,
704+
createdAt: "2026-09-10T12:00:00.000Z",
705+
updatedAt: "2026-09-10T12:00:00.000Z",
706+
attachments: [
707+
{
708+
type: "image",
709+
id: "preview",
710+
name: "preview.png",
711+
mimeType: "image/png",
712+
sizeBytes: 1,
713+
previewUrl: "blob:handoff",
714+
},
715+
],
716+
},
717+
},
718+
]),
719+
).toBe(true);
720+
expect(
721+
timelineHasEphemeralPreviewUrls([
722+
{
723+
kind: "message",
724+
message: {
725+
id: MessageId.make("preview-message"),
726+
role: "user",
727+
text: "Preview",
728+
turnId: null,
729+
streaming: false,
730+
createdAt: "2026-09-10T12:00:00.000Z",
731+
updatedAt: "2026-09-10T12:00:00.000Z",
732+
attachments: [
733+
{
734+
type: "image",
735+
id: "preview",
736+
name: "preview.png",
737+
mimeType: "image/png",
738+
sizeBytes: 1,
739+
previewUrl: "https://cdn.example/a.png",
740+
},
741+
],
742+
},
743+
},
744+
]),
745+
).toBe(false);
746+
});
747+
});
748+
568749
describe("shouldReleaseTimelineAnchorForToolActivity", () => {
569750
const activeTurnId = TurnId.make("active-turn");
570751
const anchorMessageId = MessageId.make("anchored-message");

‎apps/web/src/components/ChatView.logic.ts‎

Lines changed: 141 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import {
1818
type ThreadLinkedPullRequest,
1919
type TurnId,
2020
} from "@t3tools/contracts";
21+
import { parseScopedThreadKey } from "@t3tools/client-runtime/environment";
2122
import { resolveAssetUrl } from "@t3tools/client-runtime/state/assets";
2223
import {
2324
squashAtomCommandFailure,
@@ -262,6 +263,135 @@ export function resolveDraftHeroState(input: {
262263
);
263264
}
264265

266+
/**
267+
* Keep painted timelines on screen across thread jumps. Remounting LegendList
268+
* (or handing it an empty first paint) punches a hole through the chat pane —
269+
* white in light mode — so cmd+1/2/3 spam flashes even when the destination
270+
* is already cached.
271+
*
272+
* Stored at module scope because ChatView remounts when the thread route
273+
* changes (same pattern as the thread-error banner session dismissals).
274+
* Remember more than the last thread so jumping back to cmd+1 does not show
275+
* cmd+3's messages, and so a cached destination can paint on the first frame.
276+
*/
277+
export type HeldThreadTimeline<T extends readonly unknown[]> = {
278+
threadKey: string | null;
279+
entries: T;
280+
markdownCwd?: string | null;
281+
workspaceRoot?: string | null;
282+
};
283+
284+
const MAX_REMEMBERED_THREAD_TIMELINES = 16;
285+
286+
let rememberedThreadTimelines = new Map<string, HeldThreadTimeline<readonly unknown[]>>();
287+
let rememberedThreadTimelineOrder: string[] = [];
288+
let lastReadyThreadKey: string | null = null;
289+
290+
function rememberThreadTimelineEntries(held: HeldThreadTimeline<readonly unknown[]>): void {
291+
if (held.threadKey === null) {
292+
return;
293+
}
294+
rememberedThreadTimelines.set(held.threadKey, held);
295+
rememberedThreadTimelineOrder = [
296+
...rememberedThreadTimelineOrder.filter((key) => key !== held.threadKey),
297+
held.threadKey,
298+
];
299+
while (rememberedThreadTimelineOrder.length > MAX_REMEMBERED_THREAD_TIMELINES) {
300+
const evicted = rememberedThreadTimelineOrder.shift();
301+
if (evicted !== undefined) {
302+
rememberedThreadTimelines.delete(evicted);
303+
}
304+
}
305+
lastReadyThreadKey = held.threadKey;
306+
}
307+
308+
export function rememberReadyThreadTimeline<T extends readonly unknown[]>(
309+
held: HeldThreadTimeline<T>,
310+
): void {
311+
if (held.threadKey === null || held.entries.length === 0) {
312+
return;
313+
}
314+
rememberThreadTimelineEntries(held);
315+
}
316+
317+
export function peekRememberedThreadTimeline<T extends readonly unknown[]>(
318+
threadKey: string | null,
319+
): T | null {
320+
if (threadKey === null) {
321+
return null;
322+
}
323+
return (rememberedThreadTimelines.get(threadKey)?.entries as T | undefined) ?? null;
324+
}
325+
326+
export function peekHeldThreadTimeline<
327+
T extends readonly unknown[],
328+
>(): HeldThreadTimeline<T> | null {
329+
if (lastReadyThreadKey === null) {
330+
return null;
331+
}
332+
const held = rememberedThreadTimelines.get(lastReadyThreadKey);
333+
if (held === undefined || held.entries.length === 0) {
334+
return null;
335+
}
336+
return held as HeldThreadTimeline<T>;
337+
}
338+
339+
export function resetHeldThreadTimeline(): void {
340+
rememberedThreadTimelines = new Map();
341+
rememberedThreadTimelineOrder = [];
342+
lastReadyThreadKey = null;
343+
}
344+
345+
export function threadKeysShareEnvironment(left: string | null, right: string | null): boolean {
346+
if (left === null || right === null) {
347+
return false;
348+
}
349+
const leftRef = parseScopedThreadKey(left);
350+
const rightRef = parseScopedThreadKey(right);
351+
return leftRef !== null && rightRef !== null && leftRef.environmentId === rightRef.environmentId;
352+
}
353+
354+
/** True while we still paint another thread's last snapshot. */
355+
export function isPaintOnlyThreadTimeline(
356+
displayThreadKey: string | null,
357+
activeThreadKey: string | null,
358+
): boolean {
359+
return (
360+
displayThreadKey !== null && activeThreadKey !== null && displayThreadKey !== activeThreadKey
361+
);
362+
}
363+
364+
export function resolveThreadSwitchTimeline<T extends readonly unknown[]>(input: {
365+
loading: boolean;
366+
activeThreadKey: string | null;
367+
nextEntries: T;
368+
rememberedForActive?: T | null;
369+
lastReady?: HeldThreadTimeline<T> | null;
370+
}): { entries: T; displayThreadKey: string | null } {
371+
if (input.nextEntries.length > 0) {
372+
return { entries: input.nextEntries, displayThreadKey: input.activeThreadKey };
373+
}
374+
375+
const rememberedForActive =
376+
input.rememberedForActive ?? peekRememberedThreadTimeline<T>(input.activeThreadKey);
377+
if (input.loading && rememberedForActive !== null && rememberedForActive.length > 0) {
378+
return { entries: rememberedForActive, displayThreadKey: input.activeThreadKey };
379+
}
380+
381+
const lastReady = input.lastReady ?? peekHeldThreadTimeline<T>();
382+
if (
383+
input.loading &&
384+
lastReady !== null &&
385+
lastReady.threadKey !== null &&
386+
lastReady.threadKey !== input.activeThreadKey &&
387+
lastReady.entries.length > 0 &&
388+
threadKeysShareEnvironment(lastReady.threadKey, input.activeThreadKey)
389+
) {
390+
return { entries: lastReady.entries, displayThreadKey: lastReady.threadKey };
391+
}
392+
return { entries: input.nextEntries, displayThreadKey: input.activeThreadKey };
393+
}
394+
265395
export function resolveDraftPromotionNavigationTarget(input: {
266396
serverThreadRef: ScopedThreadRef | null;
267397
serverThread: Pick<Thread, "latestTurn" | "session"> | null | undefined;
@@ -612,6 +742,17 @@ export function revokeUserMessagePreviewUrls(message: ChatMessage): void {
612742
}
613743
}
614744

745+
export function timelineHasEphemeralPreviewUrls(
746+
entries: ReadonlyArray<Pick<TimelineEntry, "kind"> & { message?: ChatMessage }>,
747+
): boolean {
748+
return entries.some(
749+
(entry) =>
750+
entry.kind === "message" &&
751+
entry.message !== undefined &&
752+
collectUserMessageBlobPreviewUrls(entry.message).length > 0,
753+
);
754+
}
755+
615756
export function collectUserMessageBlobPreviewUrls(message: ChatMessage): string[] {
616757
if (message.role !== "user" || !message.attachments) {
617758
return [];

0 commit comments

Comments
 (0)