Skip to content

Commit fb621e4

Browse files
perf(web): the open thread's chat chrome no longer re-renders on every streaming tick
While a run streams, the server bumps the thread shell's updatedAt, runtime.updatedAt and item counts several times a second. ChatView, BranchToolbar, the branch selector and the other active-thread readers subscribed to the whole shell, so each tick re-rendered the chat chrome. Add quietThreadShellAtom, which keeps the previous shell while only those streaming fields changed during an active run, and move the visit watermark (the one updatedAt reader) into a leaf that keeps the full shell. Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent 2fbd431 commit fb621e4

17 files changed

Lines changed: 213 additions & 93 deletions

‎apps/web/src/browserFaviconStore.test.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { scopeProjectRef } from "@t3tools/client-runtime/environment";
22
import { EnvironmentId, ProjectId, ThreadId } from "@t3tools/contracts";
33
import { afterEach, beforeEach, describe, expect, it, vi } from "vite-plus/test";
44

5-
vi.mock("~/state/entities", () => ({ useThreadShell: () => null }));
5+
vi.mock("~/state/entities", () => ({ useQuietThreadShell: () => null }));
66
vi.mock("~/state/session", () => ({ usePreparedConnection: () => ({ _tag: "None" }) }));
77

88
import {

‎apps/web/src/browserFaviconStore.ts‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import { create } from "zustand";
99
import { createJSONStorage, persist } from "zustand/middleware";
1010

1111
import { normalizeHostname } from "~/browser/browserTargetResolver";
12-
import { useThreadShell } from "~/state/entities";
12+
import { useQuietThreadShell } from "~/state/entities";
1313
import { usePreparedConnection } from "~/state/session";
1414

1515
import {
@@ -196,7 +196,7 @@ export function registerFaviconProjectForThread(
196196
}
197197

198198
export function useFaviconProjectRefForThread(threadRef: ScopedThreadRef): ScopedProjectRef | null {
199-
const shell = useThreadShell(threadRef);
199+
const shell = useQuietThreadShell(threadRef);
200200
const shellProjectId = shell?.projectId ?? null;
201201
const environmentId = threadRef.environmentId;
202202
const shellProjectRef = shellProjectId ? scopeProjectRef(environmentId, shellProjectId) : null;

‎apps/web/src/components/BranchToolbar.panel.test.tsx‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ vi.mock("../composerDraftStore", () => ({
2323
select({ getDraftThreadByRef: () => null, setDraftThreadContext: vi.fn() }),
2424
}));
2525
vi.mock("../state/entities", () => ({
26-
useThreadShell: () => ({
26+
useQuietThreadShell: () => ({
2727
environmentId: "local",
2828
projectId: "project",
2929
worktreePath: state.worktreePath,

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ import {
2525

2626
import { useComposerDraftStore, type DraftId } from "../composerDraftStore";
2727
import { EnvironmentMachineIcon } from "./EnvironmentMachineIcon";
28-
import { useProject, useThreadShell, useThreadShellsForProjectRefs } from "../state/entities";
28+
import { useProject, useQuietThreadShell, useThreadShellsForProjectRefs } from "../state/entities";
2929
import {
3030
type EnvMode,
3131
type EnvironmentOption,
@@ -587,7 +587,7 @@ export const BranchToolbar = memo(function BranchToolbar({
587587
const draftThread = useComposerDraftStore((store) =>
588588
draftId ? store.getDraftSession(draftId) : store.getDraftThreadByRef(threadRef),
589589
);
590-
const serverThread = useThreadShell(threadRef);
590+
const serverThread = useQuietThreadShell(threadRef);
591591
const setDraftThreadContext = useComposerDraftStore((store) => store.setDraftThreadContext);
592592
const activeProjectRef = serverThread
593593
? scopeProjectRef(serverThread.environmentId, serverThread.projectId)

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ import { writeTextToClipboard } from "../hooks/useCopyToClipboard";
3333
import { readLocalApi } from "../localApi";
3434
import { useOpenPrLink } from "../lib/openPullRequestLink";
3535
import { usePaginatedBranches } from "../state/queries";
36-
import { useProject, useThreadShell } from "../state/entities";
36+
import { useProject, useQuietThreadShell } from "../state/entities";
3737
import { useEnvironmentQuery } from "../state/query";
3838
import { readEnvironmentScope, useEnvironmentScope } from "~/state/session";
3939
import { threadEnvironment } from "../state/threads";
@@ -130,7 +130,7 @@ export function BranchToolbarBranchSelector({
130130
reportFailure: false,
131131
});
132132
const threadRef = scopeThreadRef(environmentId, threadId);
133-
const serverThread = useThreadShell(threadRef);
133+
const serverThread = useQuietThreadShell(threadRef);
134134
const serverSession = serverThread?.runtime ?? null;
135135
const draftThread = useComposerDraftStore((store) =>
136136
draftId ? store.getDraftSession(draftId) : store.getDraftThreadByRef(threadRef),

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

Lines changed: 78 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -374,6 +374,7 @@ import {
374374
useThreadProjection,
375375
useThreadStatus,
376376
useThreadHistory,
377+
useQuietThreadShell,
377378
useThreadShell,
378379
useChildThreadInputs,
379380
useThreadRefs,
@@ -1108,7 +1109,7 @@ export default function ChatView(props: ChatViewProps) {
11081109
? store.getDraftSession(draftId)
11091110
: null,
11101111
);
1111-
const serverThread = useThreadShell(routeThreadRef);
1112+
const serverThread = useQuietThreadShell(routeThreadRef);
11121113
const routeThreadDetailRef = resolveThreadDetailRef(routeThreadRef, {
11131114
shellExists: serverThread !== null,
11141115
waitForShell: draftThread !== null,
@@ -1173,13 +1174,6 @@ export default function ChatView(props: ChatViewProps) {
11731174
}
11741175
return ids;
11751176
})();
1176-
const markThreadVisited = useUiStateStore((store) => store.markThreadVisited);
1177-
const activeThreadLocalLastVisitedAt = useUiStateStore(
1178-
(store) => store.threadLastVisitedAtById[routeThreadKey],
1179-
);
1180-
const visitThreadMutation = useAtomCommand(threadEnvironment.visit, { reportFailure: false });
1181-
const lastDispatchedVisitRef = useRef<string | null>(null);
1182-
const lastVisitDispatchAtRef = useRef(0);
11831177
const settings = useEnvironmentSettings(environmentId);
11841178
const clientSettingsHydrated = useClientSettingsHydrated();
11851179
const setStickyComposerModelSelection = useComposerDraftStore(
@@ -1501,7 +1495,7 @@ export default function ChatView(props: ChatViewProps) {
15011495
}
15021496
return scopeThreadRef(parentSubagentEnvironmentId, parentSubagentThreadId);
15031497
})();
1504-
const parentSubagentThread = useThreadShell(parentSubagentThreadRef);
1498+
const parentSubagentThread = useQuietThreadShell(parentSubagentThreadRef);
15051499
const parentThreadLink =
15061500
parentSubagentThreadRef === null
15071501
? null
@@ -1578,7 +1572,7 @@ export default function ChatView(props: ChatViewProps) {
15781572
container: workspaceLayoutElement,
15791573
widthStorageKey: `t3code:preview-panel-width:${activeThreadKey}`,
15801574
});
1581-
const activeThreadShell = useThreadShell(isServerThread ? activeThreadRef : null);
1575+
const activeThreadShell = useQuietThreadShell(isServerThread ? activeThreadRef : null);
15821576
const timelineThreadError =
15831577
serverRuntime?.status === "failed" &&
15841578
serverRuntime.lastErrorClass === "usage_limit" &&
@@ -2176,63 +2170,6 @@ export default function ChatView(props: ChatViewProps) {
21762170
};
21772171
const handlePreparedPullRequestThreadStable = useStableCallback(handlePreparedPullRequestThread);
21782172

2179-
useEffect(() => {
2180-
if (!serverThread?.id) return;
2181-
const threadUpdatedAt = Date.parse(serverThread.updatedAt);
2182-
if (Number.isNaN(threadUpdatedAt)) return;
2183-
const effectiveLastVisitedAt = resolveThreadLastVisitedAt(
2184-
serverThread.lastVisitedAt,
2185-
activeThreadLocalLastVisitedAt,
2186-
);
2187-
const lastVisitedAt = effectiveLastVisitedAt ? Date.parse(effectiveLastVisitedAt) : NaN;
2188-
if (!Number.isNaN(lastVisitedAt) && lastVisitedAt >= threadUpdatedAt) return;
2189-
2190-
if (serverThread.lastVisitedAt !== undefined) {
2191-
// Dedupe per watermark — the effect re-runs before the command's echo
2192-
// lands. The dedupe also keeps a mark-unread on the open thread sticky:
2193-
// the rewind leaves updatedAt untouched, so the already-dispatched key
2194-
// skips a fresh visit until new activity lands or the thread is reopened.
2195-
const dispatchKey = `${routeThreadKey}:${serverThread.updatedAt}`;
2196-
if (lastDispatchedVisitRef.current === dispatchKey) return;
2197-
const dispatch = () => {
2198-
lastDispatchedVisitRef.current = dispatchKey;
2199-
lastVisitDispatchAtRef.current = Date.now();
2200-
void visitThreadMutation({
2201-
environmentId: serverThread.environmentId,
2202-
input: { threadId: serverThread.id, visitedAt: serverThread.updatedAt },
2203-
});
2204-
};
2205-
const latestRunCompletedAtMs = serverThread.latestRun?.completedAt
2206-
? Date.parse(serverThread.latestRun.completedAt)
2207-
: NaN;
2208-
const hasUnseenCompletion =
2209-
!Number.isNaN(latestRunCompletedAtMs) &&
2210-
(Number.isNaN(lastVisitedAt) || latestRunCompletedAtMs > lastVisitedAt);
2211-
const elapsed = Date.now() - lastVisitDispatchAtRef.current;
2212-
if (hasUnseenCompletion || elapsed >= VISIT_DISPATCH_THROTTLE_MS) {
2213-
dispatch();
2214-
return;
2215-
}
2216-
const timer = setTimeout(dispatch, VISIT_DISPATCH_THROTTLE_MS - elapsed);
2217-
return () => clearTimeout(timer);
2218-
}
2219-
2220-
markThreadVisited(
2221-
scopedThreadKey(scopeThreadRef(serverThread.environmentId, serverThread.id)),
2222-
serverThread.updatedAt,
2223-
);
2224-
}, [
2225-
activeThreadLocalLastVisitedAt,
2226-
markThreadVisited,
2227-
routeThreadKey,
2228-
serverThread?.environmentId,
2229-
serverThread?.id,
2230-
serverThread?.lastVisitedAt,
2231-
serverThread?.latestRun?.completedAt,
2232-
serverThread?.updatedAt,
2233-
visitThreadMutation,
2234-
]);
2235-
22362173
const serverConfig = activeThread
22372174
? (activeEnvironment?.serverConfig ?? null)
22382175
: (primaryEnvironment?.serverConfig ?? null);
@@ -8775,6 +8712,7 @@ export default function ChatView(props: ChatViewProps) {
87758712
</AlertDialogPopup>
87768713
</AlertDialog>
87778714
<LinkPullRequestDialogHost />
8715+
<ThreadVisitWatermark threadRef={routeThreadRef} />
87788716
{expandedImage && (
87798717
<ExpandedImageDialog
87808718
key={expandedImageKey(expandedImage)}
@@ -8799,3 +8737,76 @@ function PreviewSessionSync(props: { readonly threadRef: ScopedThreadRef }) {
87998737
usePreviewSession(props.threadRef);
88008738
return null;
88018739
}
8740+
8741+
// The visit watermark is the one part of the chat view that follows the
8742+
// shell's updatedAt, which ticks while a run streams. Keeping it in a leaf lets
8743+
// ChatView read the quiet shell.
8744+
function ThreadVisitWatermark(props: { readonly threadRef: ScopedThreadRef }) {
8745+
const serverThread = useThreadShell(props.threadRef);
8746+
const threadKey = scopedThreadKey(props.threadRef);
8747+
const markThreadVisited = useUiStateStore((store) => store.markThreadVisited);
8748+
const activeThreadLocalLastVisitedAt = useUiStateStore(
8749+
(store) => store.threadLastVisitedAtById[threadKey],
8750+
);
8751+
const visitThreadMutation = useAtomCommand(threadEnvironment.visit, { reportFailure: false });
8752+
const lastDispatchedVisitRef = useRef<string | null>(null);
8753+
const lastVisitDispatchAtRef = useRef(0);
8754+
8755+
useEffect(() => {
8756+
if (!serverThread?.id) return;
8757+
const threadUpdatedAt = Date.parse(serverThread.updatedAt);
8758+
if (Number.isNaN(threadUpdatedAt)) return;
8759+
const effectiveLastVisitedAt = resolveThreadLastVisitedAt(
8760+
serverThread.lastVisitedAt,
8761+
activeThreadLocalLastVisitedAt,
8762+
);
8763+
const lastVisitedAt = effectiveLastVisitedAt ? Date.parse(effectiveLastVisitedAt) : NaN;
8764+
if (!Number.isNaN(lastVisitedAt) && lastVisitedAt >= threadUpdatedAt) return;
8765+
8766+
if (serverThread.lastVisitedAt !== undefined) {
8767+
// Dedupe per watermark — the effect re-runs before the command's echo
8768+
// lands. The dedupe also keeps a mark-unread on the open thread sticky:
8769+
// the rewind leaves updatedAt untouched, so the already-dispatched key
8770+
// skips a fresh visit until new activity lands or the thread is reopened.
8771+
const dispatchKey = `${threadKey}:${serverThread.updatedAt}`;
8772+
if (lastDispatchedVisitRef.current === dispatchKey) return;
8773+
const dispatch = () => {
8774+
lastDispatchedVisitRef.current = dispatchKey;
8775+
lastVisitDispatchAtRef.current = Date.now();
8776+
void visitThreadMutation({
8777+
environmentId: serverThread.environmentId,
8778+
input: { threadId: serverThread.id, visitedAt: serverThread.updatedAt },
8779+
});
8780+
};
8781+
const latestRunCompletedAtMs = serverThread.latestRun?.completedAt
8782+
? Date.parse(serverThread.latestRun.completedAt)
8783+
: NaN;
8784+
const hasUnseenCompletion =
8785+
!Number.isNaN(latestRunCompletedAtMs) &&
8786+
(Number.isNaN(lastVisitedAt) || latestRunCompletedAtMs > lastVisitedAt);
8787+
const elapsed = Date.now() - lastVisitDispatchAtRef.current;
8788+
if (hasUnseenCompletion || elapsed >= VISIT_DISPATCH_THROTTLE_MS) {
8789+
dispatch();
8790+
return;
8791+
}
8792+
const timer = setTimeout(dispatch, VISIT_DISPATCH_THROTTLE_MS - elapsed);
8793+
return () => clearTimeout(timer);
8794+
}
8795+
8796+
markThreadVisited(
8797+
scopedThreadKey(scopeThreadRef(serverThread.environmentId, serverThread.id)),
8798+
serverThread.updatedAt,
8799+
);
8800+
}, [
8801+
activeThreadLocalLastVisitedAt,
8802+
markThreadVisited,
8803+
threadKey,
8804+
serverThread?.environmentId,
8805+
serverThread?.id,
8806+
serverThread?.lastVisitedAt,
8807+
serverThread?.latestRun?.completedAt,
8808+
serverThread?.updatedAt,
8809+
visitThreadMutation,
8810+
]);
8811+
return null;
8812+
}

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ import {
4646
import { PREFERRED_HIGHLIGHTER } from "../lib/syntaxHighlighting";
4747
import { areAllDiffFilesCollapsed, toggleAllDiffFiles } from "../lib/diffCollapse";
4848
import { useWorkspaceMutationRefresh } from "../hooks/useWorkspaceMutationRefresh";
49-
import { useProject, useThreadCheckpointSummaries, useThreadShell } from "../state/entities";
49+
import { useProject, useThreadCheckpointSummaries, useQuietThreadShell } from "../state/entities";
5050
import { resolveThreadRouteRef } from "../threadRoutes";
5151
import { useClientSettings, useUpdateClientSettings } from "../hooks/useSettings";
5252
import { formatShortTimestamp } from "../timestampFormat";
@@ -220,7 +220,7 @@ export default function DiffPanel({
220220
select: (params) => resolveThreadRouteRef(params),
221221
});
222222
const activeThreadId = routeThreadRef?.threadId ?? null;
223-
const activeThread = useThreadShell(routeThreadRef);
223+
const activeThread = useQuietThreadShell(routeThreadRef);
224224
const fileAccess = useFilesystemReadAccess(activeThread?.environmentId ?? null);
225225
const { canReadFiles } = fileAccess;
226226
const activeProjectId = activeThread?.projectId ?? null;

‎apps/web/src/components/GitActionsControl.lifecycle.test.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ vi.mock("@effect/atom-react", () => ({
1818
}));
1919
vi.mock("~/state/entities", () => ({
2020
useThreadProjection: () => null,
21-
useThreadShell: (ref: { threadId: string }) => ({ branch: "main", id: ref.threadId }),
21+
useQuietThreadShell: (ref: { threadId: string }) => ({ branch: "main", id: ref.threadId }),
2222
}));
2323
vi.mock("~/state/session", () => ({
2424
useEnvironmentScope: (environmentId: unknown, scope: string) =>

‎apps/web/src/components/GitActionsControl.test.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ vi.mock("@effect/atom-react", () => ({
2727
vi.mock("~/state/entities", () => ({
2828
useThreadProjection: (ref: unknown) =>
2929
ref === null || state.detail === null ? null : { projection: { thread: state.detail } },
30-
useThreadShell: () => state.shell,
30+
useQuietThreadShell: () => state.shell,
3131
}));
3232
vi.mock("~/state/session", () => ({
3333
useEnvironmentScope: (environmentId: unknown, scope: string) =>

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ import {
9797
useVcsInitAction,
9898
useVcsPullAction,
9999
} from "~/lib/sourceControlActions";
100-
import { useThreadProjection, useThreadShell } from "~/state/entities";
100+
import { useThreadProjection, useQuietThreadShell } from "~/state/entities";
101101
import { readEnvironmentScope, useEnvironmentScope } from "~/state/session";
102102
import { useEnvironmentQuery } from "~/state/query";
103103
import { serverEnvironment } from "~/state/server";
@@ -982,7 +982,7 @@ export default function GitActionsControl({
982982
serverConfig?.availableEditors ?? [],
983983
);
984984
const threadToastData = activeThreadRef ? { threadRef: activeThreadRef } : undefined;
985-
const activeServerThreadShell = useThreadShell(activeThreadRef);
985+
const activeServerThreadShell = useQuietThreadShell(activeThreadRef);
986986
const openPrLink = useOpenPrLink(activeThreadRef ?? undefined);
987987
const activeDraftThread = useComposerDraftStore((store) =>
988988
draftId

0 commit comments

Comments
 (0)