Skip to content

Commit be73f95

Browse files
fix(web): keep thread switches correct and warm lazy panels
Rebuild the interface work from PR #18979 against current main. Preserve author credit for the original performance exploration. Co-authored-by: maria-rcks <[email protected]>
1 parent 1451e7c commit be73f95

22 files changed

Lines changed: 468 additions & 387 deletions

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

Lines changed: 11 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -59,13 +59,10 @@ import {
5959
resolveDraftHeroState,
6060
resolveExplicitDiffOpenOnActivate,
6161
resolveWorktreeSetupProgress,
62-
isPaintOnlyThreadTimeline,
63-
peekHeldThreadTimeline,
6462
peekRememberedThreadTimeline,
6563
rememberReadyThreadTimeline,
66-
resetHeldThreadTimeline,
64+
resetRememberedThreadTimelines,
6765
resolveThreadSwitchTimeline,
68-
threadKeysShareEnvironment,
6966
timelineHasEphemeralPreviewUrls,
7067
scheduleEnvironmentReconnectWarning,
7168
codexArtifactTemplatePromptToAppend,
@@ -1038,31 +1035,31 @@ describe("draft hero submission transition", () => {
10381035

10391036
describe("resolveThreadSwitchTimeline", () => {
10401037
afterEach(() => {
1041-
resetHeldThreadTimeline();
1038+
resetRememberedThreadTimelines();
10421039
});
10431040

10441041
const held = { threadKey: "env-1:thread-a", entries: ["a1", "a2"] };
10451042

1046-
it("keeps the previous thread's entries while the next thread is loading", () => {
1043+
it("leaves the destination empty while its first snapshot is loading", () => {
1044+
rememberReadyThreadTimeline(held);
10471045
expect(
10481046
resolveThreadSwitchTimeline({
10491047
loading: true,
10501048
activeThreadKey: "env-1:thread-b",
10511049
nextEntries: [],
1052-
lastReady: held,
10531050
}),
1054-
).toEqual({ entries: ["a1", "a2"], displayThreadKey: "env-1:thread-a" });
1051+
).toEqual([]);
10551052
});
10561053

10571054
it("shows the new thread once its detail is ready", () => {
1055+
rememberReadyThreadTimeline(held);
10581056
expect(
10591057
resolveThreadSwitchTimeline({
10601058
loading: false,
10611059
activeThreadKey: "env-1:thread-b",
10621060
nextEntries: ["b1"],
1063-
lastReady: held,
10641061
}),
1065-
).toEqual({ entries: ["b1"], displayThreadKey: "env-1:thread-b" });
1062+
).toEqual(["b1"]);
10661063
});
10671064

10681065
it("does not invent a timeline on the first open of a thread", () => {
@@ -1071,34 +1068,8 @@ describe("resolveThreadSwitchTimeline", () => {
10711068
loading: true,
10721069
activeThreadKey: "env-1:thread-a",
10731070
nextEntries: [],
1074-
lastReady: null,
1075-
}),
1076-
).toEqual({ entries: [], displayThreadKey: "env-1:thread-a" });
1077-
});
1078-
1079-
it("keeps the held thread workspace cwd with the snapshot", () => {
1080-
rememberReadyThreadTimeline({
1081-
...held,
1082-
markdownCwd: "/repo/a",
1083-
workspaceRoot: "/repo/a",
1084-
});
1085-
expect(peekHeldThreadTimeline<string[]>()).toEqual({
1086-
...held,
1087-
markdownCwd: "/repo/a",
1088-
workspaceRoot: "/repo/a",
1089-
});
1090-
});
1091-
1092-
it("survives a ChatView remount by remembering the last ready timeline", () => {
1093-
rememberReadyThreadTimeline(held);
1094-
expect(peekHeldThreadTimeline<string[]>()).toEqual(held);
1095-
expect(
1096-
resolveThreadSwitchTimeline({
1097-
loading: true,
1098-
activeThreadKey: "env-1:thread-b",
1099-
nextEntries: [],
11001071
}),
1101-
).toEqual({ entries: ["a1", "a2"], displayThreadKey: "env-1:thread-a" });
1072+
).toEqual([]);
11021073
});
11031074

11041075
it("paints a remembered destination instead of the last-viewed thread", () => {
@@ -1111,7 +1082,7 @@ describe("resolveThreadSwitchTimeline", () => {
11111082
activeThreadKey: "env-1:thread-a",
11121083
nextEntries: [],
11131084
}),
1114-
).toEqual({ entries: ["a1", "a2"], displayThreadKey: "env-1:thread-a" });
1085+
).toEqual(["a1", "a2"]);
11151086
});
11161087

11171088
it("prefers live entries over a remembered snapshot", () => {
@@ -1122,7 +1093,7 @@ describe("resolveThreadSwitchTimeline", () => {
11221093
activeThreadKey: "env-1:thread-b",
11231094
nextEntries: ["fresh-b"],
11241095
}),
1125-
).toEqual({ entries: ["fresh-b"], displayThreadKey: "env-1:thread-b" });
1096+
).toEqual(["fresh-b"]);
11261097
});
11271098

11281099
it("does not keep a remembered snapshot on a resolved empty thread", () => {
@@ -1133,24 +1104,7 @@ describe("resolveThreadSwitchTimeline", () => {
11331104
activeThreadKey: "env-1:thread-a",
11341105
nextEntries: [],
11351106
}),
1136-
).toEqual({ entries: [], displayThreadKey: "env-1:thread-a" });
1137-
});
1138-
1139-
it("does not hold another environment's timeline across a jump", () => {
1140-
expect(threadKeysShareEnvironment("env-1:thread-a", "env-2:thread-b")).toBe(false);
1141-
expect(
1142-
resolveThreadSwitchTimeline({
1143-
loading: true,
1144-
activeThreadKey: "env-2:thread-b",
1145-
nextEntries: [],
1146-
lastReady: held,
1147-
}),
1148-
).toEqual({ entries: [], displayThreadKey: "env-2:thread-b" });
1149-
});
1150-
1151-
it("treats a foreign held timeline as paint-only", () => {
1152-
expect(isPaintOnlyThreadTimeline("env-1:thread-a", "env-1:thread-b")).toBe(true);
1153-
expect(isPaintOnlyThreadTimeline("env-1:thread-b", "env-1:thread-b")).toBe(false);
1107+
).toEqual([]);
11541108
});
11551109

11561110
it("does not remember a timeline that still has handoff blob previews", () => {

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

Lines changed: 18 additions & 105 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@ import {
2828
} from "@t3tools/contracts";
2929
import { worktreeSetupAgentStarted } from "@t3tools/client-runtime/worktree-setup";
3030
import * as DateTime from "effect/DateTime";
31-
import { parseScopedThreadKey } from "@t3tools/client-runtime/environment";
3231
import { resolveAssetUrl } from "@t3tools/client-runtime/state/assets";
3332
import {
3433
squashAtomCommandFailure,
@@ -320,126 +319,40 @@ export function resolveDraftHeroState(input: {
320319
);
321320
}
322321

323-
/**
324-
* Remounting LegendList (or handing it an empty first paint) punches a hole
325-
* through the chat pane — white in light mode — so cmd+1/2/3 spam flashes even
326-
* when the destination is already cached.
327-
*/
328-
export type HeldThreadTimeline<T extends readonly unknown[]> = {
329-
threadKey: string | null;
330-
entries: T;
331-
markdownCwd?: string | null;
332-
workspaceRoot?: string | null;
333-
};
334-
335322
const MAX_REMEMBERED_THREAD_TIMELINES = 16;
323+
const rememberedThreadTimelines = new Map<string, readonly unknown[]>();
336324

337-
let rememberedThreadTimelines = new Map<string, HeldThreadTimeline<readonly unknown[]>>();
338-
let rememberedThreadTimelineOrder: string[] = [];
339-
let lastReadyThreadKey: string | null = null;
340-
341-
function rememberThreadTimelineEntries(held: HeldThreadTimeline<readonly unknown[]>): void {
342-
if (held.threadKey === null) {
343-
return;
344-
}
345-
rememberedThreadTimelines.set(held.threadKey, held);
346-
rememberedThreadTimelineOrder = [
347-
...rememberedThreadTimelineOrder.filter((key) => key !== held.threadKey),
348-
held.threadKey,
349-
];
350-
while (rememberedThreadTimelineOrder.length > MAX_REMEMBERED_THREAD_TIMELINES) {
351-
const evicted = rememberedThreadTimelineOrder.shift();
352-
if (evicted !== undefined) {
353-
rememberedThreadTimelines.delete(evicted);
354-
}
355-
}
356-
lastReadyThreadKey = held.threadKey;
357-
}
358-
359-
export function rememberReadyThreadTimeline<T extends readonly unknown[]>(
360-
held: HeldThreadTimeline<T>,
361-
): void {
362-
if (held.threadKey === null || held.entries.length === 0) {
363-
return;
325+
export function rememberReadyThreadTimeline<T extends readonly unknown[]>(input: {
326+
threadKey: string | null;
327+
entries: T;
328+
}): void {
329+
if (input.threadKey === null || input.entries.length === 0) return;
330+
rememberedThreadTimelines.delete(input.threadKey);
331+
rememberedThreadTimelines.set(input.threadKey, input.entries);
332+
if (rememberedThreadTimelines.size > MAX_REMEMBERED_THREAD_TIMELINES) {
333+
const oldest = rememberedThreadTimelines.keys().next().value;
334+
if (oldest !== undefined) rememberedThreadTimelines.delete(oldest);
364335
}
365-
rememberThreadTimelineEntries(held);
366336
}
367337

368338
export function peekRememberedThreadTimeline<T extends readonly unknown[]>(
369339
threadKey: string | null,
370340
): T | null {
371-
if (threadKey === null) {
372-
return null;
373-
}
374-
return (rememberedThreadTimelines.get(threadKey)?.entries as T | undefined) ?? null;
341+
if (threadKey === null) return null;
342+
return (rememberedThreadTimelines.get(threadKey) as T | undefined) ?? null;
375343
}
376344

377-
export function peekHeldThreadTimeline<
378-
T extends readonly unknown[],
379-
>(): HeldThreadTimeline<T> | null {
380-
if (lastReadyThreadKey === null) {
381-
return null;
382-
}
383-
const held = rememberedThreadTimelines.get(lastReadyThreadKey);
384-
if (held === undefined || held.entries.length === 0) {
385-
return null;
386-
}
387-
return held as HeldThreadTimeline<T>;
388-
}
389-
390-
export function resetHeldThreadTimeline(): void {
391-
rememberedThreadTimelines = new Map();
392-
rememberedThreadTimelineOrder = [];
393-
lastReadyThreadKey = null;
394-
}
395-
396-
export function threadKeysShareEnvironment(left: string | null, right: string | null): boolean {
397-
if (left === null || right === null) {
398-
return false;
399-
}
400-
const leftRef = parseScopedThreadKey(left);
401-
const rightRef = parseScopedThreadKey(right);
402-
return leftRef !== null && rightRef !== null && leftRef.environmentId === rightRef.environmentId;
403-
}
404-
405-
export function isPaintOnlyThreadTimeline(
406-
displayThreadKey: string | null,
407-
activeThreadKey: string | null,
408-
): boolean {
409-
return (
410-
displayThreadKey !== null && activeThreadKey !== null && displayThreadKey !== activeThreadKey
411-
);
345+
export function resetRememberedThreadTimelines(): void {
346+
rememberedThreadTimelines.clear();
412347
}
413348

414349
export function resolveThreadSwitchTimeline<T extends readonly unknown[]>(input: {
415350
loading: boolean;
416351
activeThreadKey: string | null;
417352
nextEntries: T;
418-
rememberedForActive?: T | null;
419-
lastReady?: HeldThreadTimeline<T> | null;
420-
}): { entries: T; displayThreadKey: string | null } {
421-
if (input.nextEntries.length > 0) {
422-
return { entries: input.nextEntries, displayThreadKey: input.activeThreadKey };
423-
}
424-
425-
const rememberedForActive =
426-
input.rememberedForActive ?? peekRememberedThreadTimeline<T>(input.activeThreadKey);
427-
if (input.loading && rememberedForActive !== null && rememberedForActive.length > 0) {
428-
return { entries: rememberedForActive, displayThreadKey: input.activeThreadKey };
429-
}
430-
431-
const lastReady = input.lastReady ?? peekHeldThreadTimeline<T>();
432-
if (
433-
input.loading &&
434-
lastReady !== null &&
435-
lastReady.threadKey !== null &&
436-
lastReady.threadKey !== input.activeThreadKey &&
437-
lastReady.entries.length > 0 &&
438-
threadKeysShareEnvironment(lastReady.threadKey, input.activeThreadKey)
439-
) {
440-
return { entries: lastReady.entries, displayThreadKey: lastReady.threadKey };
441-
}
442-
return { entries: input.nextEntries, displayThreadKey: input.activeThreadKey };
353+
}): T {
354+
if (input.nextEntries.length > 0 || !input.loading) return input.nextEntries;
355+
return peekRememberedThreadTimeline<T>(input.activeThreadKey) ?? input.nextEntries;
443356
}
444357

445358
export function resolveDraftPromotionNavigationTarget(input: {

0 commit comments

Comments
 (0)