Skip to content

Commit 36c74ff

Browse files
perf(web): visit marks during a reply no longer re-render ChatView
The open thread is marked visited several times while a reply streams. Each mark replaced the projection's thread, so the quiet thread atom passed it on and ChatView rendered once for the projection and once for the shell. Visit state is read from the shell, so the quiet atom now holds through a thread change that only moves lastVisitedAt and updatedAt. Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent fe5cd96 commit 36c74ff

2 files changed

Lines changed: 49 additions & 14 deletions

File tree

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

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { describe, expect, it } from "@effect/vitest";
33
import * as DateTime from "effect/DateTime";
44

55
import { v2Now, v2Projection, v2ThreadId } from "./orchestrationV2TestFixtures.ts";
6-
import { onlyStreamedTextChanged } from "./threadDetail.ts";
6+
import { sameThreadStructure } from "./threadDetail.ts";
77

88
const later = DateTime.makeUnsafe("2026-06-20T00:00:01.000Z");
99

@@ -45,30 +45,40 @@ function withItem(item: OrchestrationV2TurnItem, updatedAt = v2Now) {
4545
};
4646
}
4747

48-
describe("onlyStreamedTextChanged", () => {
48+
describe("sameThreadStructure", () => {
4949
const before = withItem(assistant);
5050

5151
it("holds through streamed text", () => {
5252
const next = withItem({ ...assistant, text: "Hello", updatedAt: later }, later);
53-
expect(onlyStreamedTextChanged(before, next)).toBe(true);
53+
expect(sameThreadStructure(before, next)).toBe(true);
5454
});
5555

5656
it("sees the item finish streaming", () => {
5757
const next = withItem({ ...assistant, text: "Hello", streaming: false, status: "completed" });
58-
expect(onlyStreamedTextChanged(before, next)).toBe(false);
58+
expect(sameThreadStructure(before, next)).toBe(false);
5959
});
6060

6161
it("sees a new item", () => {
6262
const next = { ...before, turnItems: [assistant, { ...assistant, id: TurnItemId.make("b") }] };
63-
expect(onlyStreamedTextChanged(before, next)).toBe(false);
63+
expect(sameThreadStructure(before, next)).toBe(false);
6464
});
6565

6666
it("sees other fields change on a streaming item", () => {
6767
const next = withItem({ ...assistant, text: "Hello", title: "Answer" });
68-
expect(onlyStreamedTextChanged(before, next)).toBe(false);
68+
expect(sameThreadStructure(before, next)).toBe(false);
69+
});
70+
71+
it("holds through a visit mark", () => {
72+
const thread = { ...before.thread, lastVisitedAt: later, updatedAt: later };
73+
expect(sameThreadStructure(before, { ...before, thread })).toBe(true);
74+
});
75+
76+
it("sees other thread changes", () => {
77+
const thread = { ...before.thread, title: "Renamed" };
78+
expect(sameThreadStructure(before, { ...before, thread })).toBe(false);
6979
});
7080

7181
it("sees changes outside the turn items", () => {
72-
expect(onlyStreamedTextChanged(before, { ...before, runs: [] })).toBe(false);
82+
expect(sameThreadStructure(before, { ...before, runs: [] })).toBe(false);
7383
});
7484
});

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

Lines changed: 32 additions & 7 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,
@@ -32,6 +33,7 @@ const EMPTY_VISIBLE_TURN_ITEMS: OrchestrationV2ThreadProjection["visibleTurnItem
3233
const EMPTY_CHECKPOINT_SUMMARIES: ReadonlyArray<ThreadCheckpointSummary> = Object.freeze([]);
3334

3435
const sameTurnItem = Schema.toEquivalence(OrchestrationV2TurnItem);
36+
const sameAppThread = Schema.toEquivalence(OrchestrationV2AppThread);
3537

3638
function sameApartFromStreamedText(
3739
previous: OrchestrationV2TurnItem,
@@ -49,19 +51,41 @@ function sameApartFromStreamedText(
4951
}
5052

5153
/**
52-
* Whether `next` differs from `previous` only by text streamed into existing
53-
* assistant and reasoning items: the same items exist, with the same status,
54-
* and every other projection field is unchanged.
54+
* Whether `next` has the structure of `previous`: it differs only by text
55+
* streamed into existing assistant and reasoning items or by a visit mark. The
56+
* same items exist, with the same status, and every other field is unchanged.
5557
*/
56-
export function onlyStreamedTextChanged(
58+
export function sameThreadStructure(
5759
previous: OrchestrationV2ThreadProjection,
5860
next: OrchestrationV2ThreadProjection,
5961
): boolean {
6062
if (previous === next) return true;
6163
for (const key of Object.keys(next) as Array<keyof OrchestrationV2ThreadProjection>) {
62-
if (key === "turnItems" || key === "visibleTurnItems" || key === "updatedAt") continue;
64+
if (
65+
key === "thread" ||
66+
key === "turnItems" ||
67+
key === "visibleTurnItems" ||
68+
key === "updatedAt"
69+
) {
70+
continue;
71+
}
6372
if (previous[key] !== next[key]) return false;
6473
}
74+
// The open thread is marked visited several times while a reply streams.
75+
// Visit state is read from the shell, so a mark is not structure.
76+
if (
77+
previous.thread !== next.thread &&
78+
!sameAppThread(
79+
{
80+
...previous.thread,
81+
updatedAt: next.thread.updatedAt,
82+
lastVisitedAt: next.thread.lastVisitedAt,
83+
},
84+
next.thread,
85+
)
86+
) {
87+
return false;
88+
}
6589
if (
6690
previous.turnItems.length !== next.turnItems.length ||
6791
previous.visibleTurnItems.length !== next.visibleTurnItems.length
@@ -122,15 +146,16 @@ export function createEnvironmentThreadDetailAtoms<E>(
122146
// Streamed assistant and reasoning text replaces an item several times a
123147
// second. Readers of the thread's structure (runs, requests, which items
124148
// exist and their status) keep the previous value through those updates;
125-
// whatever shows the text reads threadAtom or visibleTurnItemsAtom.
149+
// whatever shows the text reads threadAtom or visibleTurnItemsAtom. See
150+
// sameThreadStructure.
126151
const quietThreadAtomFamily = Atom.family((key: string) => {
127152
let previous: EnvironmentThread | null = null;
128153
return Atom.make((get) => {
129154
const next = get(threadAtomFamily(key));
130155
if (
131156
previous !== null &&
132157
next !== null &&
133-
onlyStreamedTextChanged(previous.projection, next.projection)
158+
sameThreadStructure(previous.projection, next.projection)
134159
) {
135160
return previous;
136161
}

0 commit comments

Comments
 (0)