Skip to content

Commit ee33c4c

Browse files
perf(web): visit marks during a reply no longer re-render ChatView (#23124)
Co-authored-by: Claude Opus 5.5 <[email protected]>
1 parent 83f118a commit ee33c4c

7 files changed

Lines changed: 131 additions & 25 deletions

File tree

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

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1629,14 +1629,18 @@ export function sendTitleFallbackLabels(snapshot: ComposerSendSnapshot) {
16291629

16301630
/**
16311631
* A thread shell built from its loaded detail, for direct links that hydrate the detail first.
1632-
* `updatedAt` is passed live: the thread structure leaves it out.
1632+
* `updatedAt` and `lastVisitedAt` are passed live: the thread structure leaves them out.
16331633
*/
16341634
export function presentThreadShellFromProjection(
16351635
environmentId: EnvironmentId,
16361636
threadId: ThreadId,
16371637
projection: ThreadStructure,
1638-
updatedAt: ThreadProjection["updatedAt"],
1638+
live: {
1639+
readonly updatedAt: ThreadProjection["updatedAt"];
1640+
readonly lastVisitedAt: ThreadProjection["thread"]["lastVisitedAt"];
1641+
},
16391642
) {
1643+
const { updatedAt, lastVisitedAt } = live;
16401644
const latestRun = deriveLatestThreadRun(projection);
16411645
const runtime = deriveThreadRuntime({ ...projection, updatedAt });
16421646
const pendingRuntimeRequest =
@@ -1645,6 +1649,7 @@ export function presentThreadShellFromProjection(
16451649
return {
16461650
...presentThreadShell(environmentId, {
16471651
...projection.thread,
1652+
lastVisitedAt,
16481653
latestRunId: latestRun?.runId ?? null,
16491654
activeRunId: runtime?.activeRunId ?? null,
16501655
status: runtime?.status ?? "idle",

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

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -236,6 +236,7 @@ import {
236236
useProject,
237237
useQuietThreadProjection,
238238
useThreadUpdatedAt,
239+
useThreadLastVisitedAt,
239240
useQuietThreadVisibleTurnItems,
240241
useThreadStatus,
241242
useThreadHistory,
@@ -464,19 +465,17 @@ export default function ChatView(props: ChatViewProps) {
464465
});
465466
const serverThreadProjection = useQuietThreadProjection(routeThreadDetailRef);
466467
const serverProjection = serverThreadProjection?.projection ?? null;
467-
// Only the shell-less fallback needs it; with a shell, the shell carries it.
468-
const fallbackUpdatedAt = useThreadUpdatedAt(
469-
serverThreadShell === null ? routeThreadDetailRef : null,
470-
);
468+
// Only the shell-less fallback needs these; with a shell, the shell carries them.
469+
const fallbackDetailRef = serverThreadShell === null ? routeThreadDetailRef : null;
470+
const fallbackUpdatedAt = useThreadUpdatedAt(fallbackDetailRef);
471+
const fallbackLastVisitedAt = useThreadLastVisitedAt(fallbackDetailRef);
471472
const serverThread =
472473
serverThreadShell !== null || serverProjection === null || fallbackUpdatedAt === null
473474
? serverThreadShell
474-
: presentThreadShellFromProjection(
475-
environmentId,
476-
threadId,
477-
serverProjection,
478-
fallbackUpdatedAt,
479-
);
475+
: presentThreadShellFromProjection(environmentId, threadId, serverProjection, {
476+
updatedAt: fallbackUpdatedAt,
477+
lastVisitedAt: fallbackLastVisitedAt,
478+
});
480479
const promptRef = useRef("");
481480
const localComposerRef = useRef<ChatComposerHandle | null>(null);
482481
const composerRef = useComposerHandleContext() ?? localComposerRef;

‎apps/web/src/lib/contextWindow.ts‎

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
1-
import type { ThreadStructureTurnItem } from "@t3tools/client-runtime/state/shell";
1+
import type { ThreadStructure, ThreadStructureTurnItem } from "@t3tools/client-runtime/state/shell";
22
import type {
33
OrchestrationV2ProviderTurn,
44
OrchestrationV2ProviderTurnTokenUsage,
55
OrchestrationV2ProviderThread,
6-
OrchestrationV2ThreadProjection,
76
ThreadTokenUsageSnapshot,
87
} from "@t3tools/contracts";
98
import * as DateTime from "effect/DateTime";
@@ -29,7 +28,7 @@ export type ContextWindowSnapshot = NullableContextWindowUsage & {
2928
by provider thread, not time, so pick by timestamp. */
3029
export function latestProviderTurnTokenUsage(
3130
turns: ReadonlyArray<Pick<OrchestrationV2ProviderTurn, "tokenUsage">>,
32-
projection?: Pick<OrchestrationV2ThreadProjection, "thread" | "contextTransfers"> | null,
31+
projection?: Pick<ThreadStructure, "thread" | "contextTransfers"> | null,
3332
providerThread?: Pick<OrchestrationV2ProviderThread, "contextUsage"> | null,
3433
): OrchestrationV2ProviderTurnTokenUsage | null {
3534
let latest: OrchestrationV2ProviderTurnTokenUsage | null = null;

‎apps/web/src/state/entities.ts‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,9 @@ const EMPTY_VISIBLE_TURN_ITEMS_ATOM = Atom.make(EMPTY_VISIBLE_TURN_ITEMS).pipe(
6565
const EMPTY_CHECKPOINT_SUMMARIES_ATOM = Atom.make<ReadonlyArray<ThreadCheckpointSummary>>(
6666
Object.freeze([]),
6767
).pipe(Atom.withLabel("web-thread-checkpoint-summaries:empty"));
68+
const EMPTY_LAST_VISITED_AT_ATOM = Atom.make<DateTime.Utc | null>(null).pipe(
69+
Atom.withLabel("web-thread-last-visited:empty"),
70+
);
6871
const EMPTY_UPDATED_AT_ATOM = Atom.make<DateTime.Utc | null>(null).pipe(
6972
Atom.withLabel("web-thread-updated-at:empty"),
7073
);
@@ -171,6 +174,13 @@ export function useQuietThreadProjection(
171174
);
172175
}
173176

177+
/** The detail's live visit mark, which the thread structure leaves out. */
178+
export function useThreadLastVisitedAt(ref: ScopedThreadRef | null): DateTime.Utc | null {
179+
return useAtomValue(
180+
ref === null ? EMPTY_LAST_VISITED_AT_ATOM : environmentThreadDetails.lastVisitedAtAtom(ref),
181+
);
182+
}
183+
174184
/** The detail's live `updatedAt`, which the thread structure leaves out. */
175185
export function useThreadUpdatedAt(ref: ScopedThreadRef | null): DateTime.Utc | null {
176186
return useAtomValue(

‎packages/client-runtime/src/state/models.ts‎

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,10 +58,16 @@ export type ThreadStructureVisibleTurnItem = Omit<
5858
* reader of the quiet thread cannot depend on them and go stale. A full
5959
* projection is assignable to it.
6060
*/
61+
export type ThreadStructureThread = Omit<
62+
OrchestrationV2ThreadProjection["thread"],
63+
"updatedAt" | "lastVisitedAt"
64+
>;
65+
6166
export type ThreadStructure = Omit<
6267
OrchestrationV2ThreadProjection,
63-
"turnItems" | "visibleTurnItems" | "updatedAt"
68+
"thread" | "turnItems" | "visibleTurnItems" | "updatedAt"
6469
> & {
70+
readonly thread: ThreadStructureThread;
6571
readonly turnItems: ReadonlyArray<ThreadStructureTurnItem>;
6672
readonly visibleTurnItems: ReadonlyArray<ThreadStructureVisibleTurnItem>;
6773
};

‎packages/client-runtime/src/state/threadDetail.test.ts‎

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,11 +78,48 @@ describe("sameThreadStructure", () => {
7878
expect(sameThreadStructure(before, next)).toBe(false);
7979
});
8080

81+
it("holds through a visit mark", () => {
82+
const thread = { ...before.thread, lastVisitedAt: later, updatedAt: later };
83+
expect(sameThreadStructure(before, { ...before, thread })).toBe(true);
84+
});
85+
86+
it("sees other thread changes", () => {
87+
const thread = { ...before.thread, title: "Renamed" };
88+
expect(sameThreadStructure(before, { ...before, thread })).toBe(false);
89+
});
90+
8191
it("sees changes outside the turn items", () => {
8292
expect(sameThreadStructure(before, { ...before, runs: [] })).toBe(false);
8393
});
8494
});
8595

96+
describe("lastVisitedAtAtom", () => {
97+
it("follows a visit mark the quiet thread holds through", () => {
98+
const projection = withItem(assistant);
99+
const state = Atom.make(
100+
AsyncResult.success({ ...EMPTY_ENVIRONMENT_THREAD_STATE, data: Option.some(projection) }),
101+
);
102+
const details = createEnvironmentThreadDetailAtoms(() => state);
103+
const ref = { environmentId: EnvironmentId.make("env"), threadId: v2ThreadId };
104+
const registry = AtomRegistry.make();
105+
const unmountQuiet = registry.mount(details.quietThreadAtom(ref));
106+
const unmountVisit = registry.mount(details.lastVisitedAtAtom(ref));
107+
const quietBefore = registry.get(details.quietThreadAtom(ref));
108+
109+
const visited = { ...projection, thread: { ...projection.thread, lastVisitedAt: later } };
110+
registry.set(
111+
state,
112+
AsyncResult.success({ ...EMPTY_ENVIRONMENT_THREAD_STATE, data: Option.some(visited) }),
113+
);
114+
115+
expect(registry.get(details.quietThreadAtom(ref))).toBe(quietBefore);
116+
expect(registry.get(details.lastVisitedAtAtom(ref))).toBe(later);
117+
unmountQuiet();
118+
unmountVisit();
119+
registry.dispose();
120+
});
121+
});
122+
86123
describe("quietThreadAtom", () => {
87124
const ref = { environmentId: EnvironmentId.make("environment-1"), threadId: v2ThreadId };
88125

@@ -125,6 +162,8 @@ describe("quietThreadAtom", () => {
125162
const thread = setup(withItem(assistant));
126163
const projection = thread.read()?.projection;
127164
expect(projection).not.toHaveProperty("updatedAt");
165+
expect(projection?.thread).not.toHaveProperty("updatedAt");
166+
expect(projection?.thread).not.toHaveProperty("lastVisitedAt");
128167
expect(projection?.turnItems[0]).not.toHaveProperty("text");
129168
expect(projection?.turnItems[0]).not.toHaveProperty("updatedAt");
130169
expect(projection?.visibleTurnItems[0]?.item).not.toHaveProperty("text");

‎packages/client-runtime/src/state/threadDetail.ts‎

Lines changed: 57 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import {
2+
OrchestrationV2AppThread,
23
OrchestrationV2TurnItem,
34
type OrchestrationV2ThreadProjection,
45
type ScopedThreadRef,
@@ -21,6 +22,7 @@ import type {
2122
EnvironmentThread,
2223
EnvironmentThreadStructure,
2324
ThreadStructure,
25+
ThreadStructureThread,
2426
ThreadStructureTurnItem,
2527
ThreadStructureVisibleTurnItem,
2628
} from "./models.ts";
@@ -41,6 +43,7 @@ const EMPTY_CHECKPOINT_SUMMARIES: ReadonlyArray<ThreadCheckpointSummary> = Objec
4143

4244
type TurnItem = OrchestrationV2ThreadProjection["turnItems"][number];
4345
type VisibleTurnItem = OrchestrationV2ThreadProjection["visibleTurnItems"][number];
46+
type AppThread = OrchestrationV2ThreadProjection["thread"];
4447

4548
// Items are stripped once each: unchanged items keep their identity across
4649
// structure changes, which the incremental timeline relies on.
@@ -76,45 +79,83 @@ export function isCompleteTurnItemRow(row: ThreadStructureVisibleTurnItem): row
7679
return row.item.type !== "assistant_message" && row.item.type !== "reasoning";
7780
}
7881

82+
// The open thread is marked visited several times while a reply streams. Visit
83+
// state is read from the shell, or from lastVisitedAtAtom while the thread is
84+
// not in the shell index yet, so a mark is not structure.
85+
function toThreadStructureThread(thread: AppThread): ThreadStructureThread {
86+
const { updatedAt: _updatedAt, lastVisitedAt: _lastVisitedAt, ...structure } = thread;
87+
return structure;
88+
}
89+
7990
function toThreadStructure(projection: OrchestrationV2ThreadProjection): ThreadStructure {
8091
const { updatedAt: _updatedAt, ...structure } = projection;
8192
return {
8293
...structure,
94+
thread: toThreadStructureThread(projection.thread),
8395
turnItems: projection.turnItems.map(toThreadStructureTurnItem),
8496
visibleTurnItems: projection.visibleTurnItems.map(toThreadStructureVisibleTurnItem),
8597
};
8698
}
8799

88100
const sameTurnItem = Schema.toEquivalence(OrchestrationV2TurnItem);
101+
const sameAppThread = Schema.toEquivalence(OrchestrationV2AppThread);
102+
103+
// Overlays previous's structure on next, so the fields the strip functions
104+
// remove are exactly the ones ignored here.
105+
function sameApartFromStripped<T extends object>(
106+
same: (left: T, right: T) => boolean,
107+
previous: T,
108+
previousStructure: object,
109+
next: T,
110+
): boolean {
111+
return (
112+
Object.keys(previous).length === Object.keys(next).length &&
113+
same(Object.assign({}, next, previousStructure), next)
114+
);
115+
}
89116

90-
// Compares through toThreadStructureTurnItem, so the fields it strips are
91-
// exactly the ones ignored here.
92117
function sameTurnItemStructure(previous: TurnItem, next: TurnItem): boolean {
93118
if (previous === next) return true;
94119
const structure = toThreadStructureTurnItem(previous);
95120
if (previous.id !== next.id || previous.type !== next.type || structure === previous) {
96121
return false;
97122
}
98-
return (
99-
Object.keys(previous).length === Object.keys(next).length &&
100-
sameTurnItem(Object.assign({}, next, structure), next)
101-
);
123+
return sameApartFromStripped(sameTurnItem, previous, structure, next);
102124
}
103125

104126
/**
105127
* Whether `previous` and `next` have the same ThreadStructure: they differ
106-
* only by text streamed into existing assistant and reasoning items. The same
107-
* items exist, with the same status, and every other field is unchanged.
128+
* only by text streamed into existing assistant and reasoning items or by a
129+
* visit mark. The same items exist, with the same status, and every other
130+
* field is unchanged.
108131
*/
109132
export function sameThreadStructure(
110133
previous: OrchestrationV2ThreadProjection,
111134
next: OrchestrationV2ThreadProjection,
112135
): boolean {
113136
if (previous === next) return true;
114137
for (const key of Object.keys(next) as Array<keyof OrchestrationV2ThreadProjection>) {
115-
if (key === "turnItems" || key === "visibleTurnItems" || key === "updatedAt") continue;
138+
if (
139+
key === "thread" ||
140+
key === "turnItems" ||
141+
key === "visibleTurnItems" ||
142+
key === "updatedAt"
143+
) {
144+
continue;
145+
}
116146
if (previous[key] !== next[key]) return false;
117147
}
148+
if (
149+
previous.thread !== next.thread &&
150+
!sameApartFromStripped(
151+
sameAppThread,
152+
previous.thread,
153+
toThreadStructureThread(previous.thread),
154+
next.thread,
155+
)
156+
) {
157+
return false;
158+
}
118159
if (
119160
previous.turnItems.length !== next.turnItems.length ||
120161
previous.visibleTurnItems.length !== next.visibleTurnItems.length
@@ -304,6 +345,12 @@ export function createEnvironmentThreadDetailAtoms<E>(
304345
}).pipe(Atom.setIdleTTL(0), Atom.withLabel(`environment-thread-checkpoint-summaries:${key}`));
305346
});
306347

348+
const lastVisitedAtAtomFamily = Atom.family((key: string) =>
349+
Atom.make(
350+
(get) =>
351+
Option.getOrNull(get(threadStateValueAtomFamily(key)).data)?.thread.lastVisitedAt ?? null,
352+
).pipe(Atom.setIdleTTL(0), Atom.withLabel(`environment-thread-last-visited:${key}`)),
353+
);
307354
const updatedAtAtomFamily = Atom.family((key: string) =>
308355
Atom.make(
309356
(get) => Option.getOrNull(get(threadStateValueAtomFamily(key)).data)?.updatedAt ?? null,
@@ -372,6 +419,7 @@ export function createEnvironmentThreadDetailAtoms<E>(
372419
return {
373420
worktreePathAtom: (ref: ScopedThreadRef) => worktreePathAtomFamily(threadKey(ref)),
374421
updatedAtAtom: (ref: ScopedThreadRef) => updatedAtAtomFamily(threadKey(ref)),
422+
lastVisitedAtAtom: (ref: ScopedThreadRef) => lastVisitedAtAtomFamily(threadKey(ref)),
375423
pendingRequestsAtom: (ref: ScopedThreadRef) => pendingRequestsAtomFamily(threadKey(ref)),
376424
queueWorkflowAtom: (ref: ScopedThreadRef) => queueWorkflowAtomFamily(threadKey(ref)),
377425
queuedCountAtom: (ref: ScopedThreadRef) => queuedCountAtomFamily(threadKey(ref)),

0 commit comments

Comments
 (0)