@devries-cameron20 / t3code Mirror · read-only / commits / ee33c4c65d
Fork 0

The best way to control Claude Code, Codex and more from any computer or phone

Mirrored from https://github.com/Camo67/t3code.git

Synced Nov 22, 19:30 UTC

perf(web): visit marks during a reply no longer re-render ChatView (#23124)

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>

Julius Marminge committed Oct 22, 2026 at 01:11 UTC ee33c4c65dfde09564c2c9f9da4ffdf53828cb15
7 files changed +131 -25
apps/web/src/components/ChatView.logic.ts
+7 -2
@@ -1629,14 +1629,18 @@ export function sendTitleFallbackLabels(snapshot: ComposerSendSnapshot) {
1629
1630 /**
1631 * 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.
1633 */
1634 export function presentThreadShellFromProjection(
1635 environmentId: EnvironmentId,
1636 threadId: ThreadId,
1637 projection: ThreadStructure,
1638 - updatedAt: ThreadProjection["updatedAt"],
1638 + live: {
1639 + readonly updatedAt: ThreadProjection["updatedAt"];
1640 + readonly lastVisitedAt: ThreadProjection["thread"]["lastVisitedAt"];
1641 + },
1642 ) {
1643 + const { updatedAt, lastVisitedAt } = live;
1644 const latestRun = deriveLatestThreadRun(projection);
1645 const runtime = deriveThreadRuntime({ ...projection, updatedAt });
1646 const pendingRuntimeRequest =
@@ -1645,6 +1649,7 @@ export function presentThreadShellFromProjection(
1649 return {
1650 ...presentThreadShell(environmentId, {
1651 ...projection.thread,
1652 + lastVisitedAt,
1653 latestRunId: latestRun?.runId ?? null,
1654 activeRunId: runtime?.activeRunId ?? null,
1655 status: runtime?.status ?? "idle",
apps/web/src/components/ChatView.tsx
+9 -10
@@ -236,6 +236,7 @@ import {
236 useProject,
237 useQuietThreadProjection,
238 useThreadUpdatedAt,
239 + useThreadLastVisitedAt,
240 useQuietThreadVisibleTurnItems,
241 useThreadStatus,
242 useThreadHistory,
@@ -464,19 +465,17 @@ export default function ChatView(props: ChatViewProps) {
465 });
466 const serverThreadProjection = useQuietThreadProjection(routeThreadDetailRef);
467 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);
472 const serverThread =
473 serverThreadShell !== null || serverProjection === null || fallbackUpdatedAt === null
474 ? serverThreadShell
474 - : presentThreadShellFromProjection(
475 - environmentId,
476 - threadId,
477 - serverProjection,
478 - fallbackUpdatedAt,
479 - );
475 + : presentThreadShellFromProjection(environmentId, threadId, serverProjection, {
476 + updatedAt: fallbackUpdatedAt,
477 + lastVisitedAt: fallbackLastVisitedAt,
478 + });
479 const promptRef = useRef("");
480 const localComposerRef = useRef<ChatComposerHandle | null>(null);
481 const composerRef = useComposerHandleContext() ?? localComposerRef;
apps/web/src/lib/contextWindow.ts
+2 -3
@@ -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";
2 import type {
3 OrchestrationV2ProviderTurn,
4 OrchestrationV2ProviderTurnTokenUsage,
5 OrchestrationV2ProviderThread,
6 - OrchestrationV2ThreadProjection,
6 ThreadTokenUsageSnapshot,
7 } from "@t3tools/contracts";
8 import * as DateTime from "effect/DateTime";
@@ -29,7 +28,7 @@ export type ContextWindowSnapshot = NullableContextWindowUsage & {
28 by provider thread, not time, so pick by timestamp. */
29 export function latestProviderTurnTokenUsage(
30 turns: ReadonlyArray<Pick<OrchestrationV2ProviderTurn, "tokenUsage">>,
32 - projection?: Pick<OrchestrationV2ThreadProjection, "thread" | "contextTransfers"> | null,
31 + projection?: Pick<ThreadStructure, "thread" | "contextTransfers"> | null,
32 providerThread?: Pick<OrchestrationV2ProviderThread, "contextUsage"> | null,
33 ): OrchestrationV2ProviderTurnTokenUsage | null {
34 let latest: OrchestrationV2ProviderTurnTokenUsage | null = null;
apps/web/src/state/entities.ts
+10
@@ -65,6 +65,9 @@ const EMPTY_VISIBLE_TURN_ITEMS_ATOM = Atom.make(EMPTY_VISIBLE_TURN_ITEMS).pipe(
65 const EMPTY_CHECKPOINT_SUMMARIES_ATOM = Atom.make<ReadonlyArray<ThreadCheckpointSummary>>(
66 Object.freeze([]),
67 ).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 +);
71 const EMPTY_UPDATED_AT_ATOM = Atom.make<DateTime.Utc | null>(null).pipe(
72 Atom.withLabel("web-thread-updated-at:empty"),
73 );
@@ -171,6 +174,13 @@ export function useQuietThreadProjection(
174 );
175 }
176
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 +
184 /** The detail's live `updatedAt`, which the thread structure leaves out. */
185 export function useThreadUpdatedAt(ref: ScopedThreadRef | null): DateTime.Utc | null {
186 return useAtomValue(
packages/client-runtime/src/state/models.ts
+7 -1
@@ -58,10 +58,16 @@ export type ThreadStructureVisibleTurnItem = Omit<
58 * reader of the quiet thread cannot depend on them and go stale. A full
59 * projection is assignable to it.
60 */
61 +export type ThreadStructureThread = Omit<
62 + OrchestrationV2ThreadProjection["thread"],
63 + "updatedAt" | "lastVisitedAt"
64 +>;
65 +
66 export type ThreadStructure = Omit<
67 OrchestrationV2ThreadProjection,
63 - "turnItems" | "visibleTurnItems" | "updatedAt"
68 + "thread" | "turnItems" | "visibleTurnItems" | "updatedAt"
69 > & {
70 + readonly thread: ThreadStructureThread;
71 readonly turnItems: ReadonlyArray<ThreadStructureTurnItem>;
72 readonly visibleTurnItems: ReadonlyArray<ThreadStructureVisibleTurnItem>;
73 };
packages/client-runtime/src/state/threadDetail.test.ts
+39
@@ -78,11 +78,48 @@ describe("sameThreadStructure", () => {
78 expect(sameThreadStructure(before, next)).toBe(false);
79 });
80
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 +
91 it("sees changes outside the turn items", () => {
92 expect(sameThreadStructure(before, { ...before, runs: [] })).toBe(false);
93 });
94 });
95
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 +
123 describe("quietThreadAtom", () => {
124 const ref = { environmentId: EnvironmentId.make("environment-1"), threadId: v2ThreadId };
125
@@ -125,6 +162,8 @@ describe("quietThreadAtom", () => {
162 const thread = setup(withItem(assistant));
163 const projection = thread.read()?.projection;
164 expect(projection).not.toHaveProperty("updatedAt");
165 + expect(projection?.thread).not.toHaveProperty("updatedAt");
166 + expect(projection?.thread).not.toHaveProperty("lastVisitedAt");
167 expect(projection?.turnItems[0]).not.toHaveProperty("text");
168 expect(projection?.turnItems[0]).not.toHaveProperty("updatedAt");
169 expect(projection?.visibleTurnItems[0]?.item).not.toHaveProperty("text");
packages/client-runtime/src/state/threadDetail.ts
+57 -9
@@ -1,4 +1,5 @@
1 import {
2 + OrchestrationV2AppThread,
3 OrchestrationV2TurnItem,
4 type OrchestrationV2ThreadProjection,
5 type ScopedThreadRef,
@@ -21,6 +22,7 @@ import type {
22 EnvironmentThread,
23 EnvironmentThreadStructure,
24 ThreadStructure,
25 + ThreadStructureThread,
26 ThreadStructureTurnItem,
27 ThreadStructureVisibleTurnItem,
28 } from "./models.ts";
@@ -41,6 +43,7 @@ const EMPTY_CHECKPOINT_SUMMARIES: ReadonlyArray<ThreadCheckpointSummary> = Objec
43
44 type TurnItem = OrchestrationV2ThreadProjection["turnItems"][number];
45 type VisibleTurnItem = OrchestrationV2ThreadProjection["visibleTurnItems"][number];
46 +type AppThread = OrchestrationV2ThreadProjection["thread"];
47
48 // Items are stripped once each: unchanged items keep their identity across
49 // structure changes, which the incremental timeline relies on.
@@ -76,35 +79,55 @@ export function isCompleteTurnItemRow(row: ThreadStructureVisibleTurnItem): row
79 return row.item.type !== "assistant_message" && row.item.type !== "reasoning";
80 }
81
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 +
90 function toThreadStructure(projection: OrchestrationV2ThreadProjection): ThreadStructure {
91 const { updatedAt: _updatedAt, ...structure } = projection;
92 return {
93 ...structure,
94 + thread: toThreadStructureThread(projection.thread),
95 turnItems: projection.turnItems.map(toThreadStructureTurnItem),
96 visibleTurnItems: projection.visibleTurnItems.map(toThreadStructureVisibleTurnItem),
97 };
98 }
99
100 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 +}
116
90 -// Compares through toThreadStructureTurnItem, so the fields it strips are
91 -// exactly the ones ignored here.
117 function sameTurnItemStructure(previous: TurnItem, next: TurnItem): boolean {
118 if (previous === next) return true;
119 const structure = toThreadStructureTurnItem(previous);
120 if (previous.id !== next.id || previous.type !== next.type || structure === previous) {
121 return false;
122 }
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);
124 }
125
126 /**
127 * 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.
131 */
132 export function sameThreadStructure(
133 previous: OrchestrationV2ThreadProjection,
@@ -112,9 +135,27 @@ export function sameThreadStructure(
135 ): boolean {
136 if (previous === next) return true;
137 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 + }
146 if (previous[key] !== next[key]) return false;
147 }
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 + }
159 if (
160 previous.turnItems.length !== next.turnItems.length ||
161 previous.visibleTurnItems.length !== next.visibleTurnItems.length
@@ -304,6 +345,12 @@ export function createEnvironmentThreadDetailAtoms<E>(
345 }).pipe(Atom.setIdleTTL(0), Atom.withLabel(`environment-thread-checkpoint-summaries:${key}`));
346 });
347
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 + );
354 const updatedAtAtomFamily = Atom.family((key: string) =>
355 Atom.make(
356 (get) => Option.getOrNull(get(threadStateValueAtomFamily(key)).data)?.updatedAt ?? null,
@@ -372,6 +419,7 @@ export function createEnvironmentThreadDetailAtoms<E>(
419 return {
420 worktreePathAtom: (ref: ScopedThreadRef) => worktreePathAtomFamily(threadKey(ref)),
421 updatedAtAtom: (ref: ScopedThreadRef) => updatedAtAtomFamily(threadKey(ref)),
422 + lastVisitedAtAtom: (ref: ScopedThreadRef) => lastVisitedAtAtomFamily(threadKey(ref)),
423 pendingRequestsAtom: (ref: ScopedThreadRef) => pendingRequestsAtomFamily(threadKey(ref)),
424 queueWorkflowAtom: (ref: ScopedThreadRef) => queueWorkflowAtomFamily(threadKey(ref)),
425 queuedCountAtom: (ref: ScopedThreadRef) => queuedCountAtomFamily(threadKey(ref)),