Skip to content

Commit bb23117

Browse files
perf(web): a streaming thread re-renders only its own sidebar row
The sidebar and its project grouping subscribed to every thread shell, so each shell update from a running thread re-rendered the whole sidebar body, every sortable row wrapper and every shelf marker. They now read quiet shells, which hold still while only streaming timestamps change, and each row reads its own live shell. Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent 9179178 commit bb23117

6 files changed

Lines changed: 70 additions & 6 deletions

File tree

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ import {
116116
useAllEnvironmentProjectSnapshotsReady,
117117
useProjects,
118118
useServerConfigs,
119-
useThreadShells,
119+
useQuietThreadShells,
120120
} from "../state/entities";
121121
import { environmentPresentations } from "../state/presentation";
122122
import { primaryServerKeybindingsAtom } from "../state/server";
@@ -356,7 +356,7 @@ function useSidebarThreads(input: {
356356
}) {
357357
const { projectScopeKey, scopedProjectKeys, serverConfigs, optimisticDrop, routeThreadRef } =
358358
input;
359-
const threads = useThreadShells();
359+
const threads = useQuietThreadShells();
360360
const routeRowThreadKey =
361361
routeThreadRef === null
362362
? null

‎apps/web/src/components/sidebar/SidebarThreadRow.tsx‎

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,7 @@ import {
9696
ThreadSearchPullRequestNumber,
9797
} from "../ThreadStatusIndicators";
9898
import { resolveSnoozePresets, type SnoozePreset } from "../Sidebar.snooze";
99+
import { useThreadShell } from "../../state/entities";
99100
import { ProjectFavicon, type ProjectFaviconProject } from "../ProjectFavicon";
100101
import { ThreadSearchMatchExcerpt } from "../ThreadSearchMatch";
101102
import { makeWorkspaceFileDropHandlers } from "../chat/workspaceFileDrop";
@@ -463,8 +464,13 @@ export function SortableThreadRow(
463464
transition: contextDrag && isDragging ? "transform 150ms ease-out" : transition,
464465
isDragging: isDragging && !contextDrag,
465466
};
467+
// The sidebar arranges rows from quiet shells, which hold still while a
468+
// thread streams; reading the live shell here re-renders only this row.
469+
const thread =
470+
useThreadShell(scopeThreadRef(rowProps.thread.environmentId, rowProps.thread.id)) ??
471+
rowProps.thread;
466472
// Keyed by variant so a card/slim switch remounts the row.
467-
return <SidebarThreadRow key={rowProps.variant} {...rowProps} sortable={bag} />;
473+
return <SidebarThreadRow key={rowProps.variant} {...rowProps} thread={thread} sortable={bag} />;
468474
}
469475
export type SidebarSweepAction = "settle" | "unsettle" | "unsnooze";
470476
const dropVerbBadge: Record<SidebarDropVerb, ReactNode> = {
@@ -1508,7 +1514,8 @@ export const SidebarSearchResultRow = memo(function SidebarSearchResultRow(props
15081514
onSelect: () => void;
15091515
onFileDropThreads: (threadRef: ScopedThreadRef, files: File[]) => void;
15101516
}) {
1511-
const { thread } = props;
1517+
const thread =
1518+
useThreadShell(scopeThreadRef(props.thread.environmentId, props.thread.id)) ?? props.thread;
15121519
const accessibility = resolveSidebarRowAccessibility({
15131520
title: thread.title,
15141521
statusLabel: null,

‎apps/web/src/hooks/useSidebarProjectGroups.ts‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,15 @@ import { orderItemsByPreferredIds, sortSidebarV2ProjectGroups } from "../compone
22
import { getProjectOrderKey, selectProjectGroupingSettings } from "../logicalProject";
33
import { buildSidebarProjectSnapshots } from "../sidebarProjectGrouping";
44
import { useEnvironmentIdentities, usePrimaryEnvironmentId } from "../state/environments";
5-
import { useProjects, useThreadShells } from "../state/entities";
5+
import { useProjects, useQuietThreadShells } from "../state/entities";
66
import { legacyProjectCwdPreferenceKey, useUiStateStore } from "../uiStateStore";
77
import { useClientSettings } from "./useSettings";
88

99
export function useSidebarProjectGroups() {
1010
const projects = useProjects();
11-
const threads = useThreadShells();
11+
// Activity order lags a running thread's streaming ticks until its next
12+
// real change; reading every tick re-rendered the whole sidebar with it.
13+
const threads = useQuietThreadShells();
1214
const projectOrder = useUiStateStore((store) => store.projectOrder);
1315
const sortOrder = useClientSettings((s) => s.sidebarProjectSortOrder);
1416
const groupingSettings = useClientSettings(selectProjectGroupingSettings);

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,11 @@ export function useQuietThreadShell(ref: ScopedThreadRef | null): EnvironmentThr
139139
);
140140
}
141141

142+
/** Every thread shell, held through streaming ticks; see quietThreadShellAtom. */
143+
export function useQuietThreadShells(): ReadonlyArray<EnvironmentThreadShell> {
144+
return useAtomValue(environmentThreadShells.quietThreadShellsAtom);
145+
}
146+
142147
export function useChildThreadInputs(ref: ScopedThreadRef | null) {
143148
return useAtomValue(
144149
ref === null ? EMPTY_THREAD_SHELLS_ATOM : environmentThreadShells.childThreadInputsAtom(ref),

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

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -344,4 +344,39 @@ describe("v2 thread shell lists", () => {
344344
harness.registry.dispose();
345345
}
346346
});
347+
348+
it("keeps the quiet shell list through streaming ticks", () => {
349+
const harness = makeHarness();
350+
const runId = RunId.make("streaming-list-run");
351+
const running: OrchestrationV2ThreadShell = {
352+
...v2ThreadShell,
353+
latestRunId: runId,
354+
activeRunId: runId,
355+
status: "running",
356+
itemCount: 1,
357+
visibleItemCount: 1,
358+
};
359+
const setThread = (thread: OrchestrationV2ThreadShell, sequence: number) =>
360+
harness.registry.set(harness.snapshotAtom(environmentId), {
361+
...v2ShellSnapshot,
362+
snapshotSequence: sequence,
363+
threads: [thread],
364+
});
365+
setThread(running, 1);
366+
const listAtom = harness.threads.quietThreadShellsAtom;
367+
const dispose = harness.registry.mount(listAtom);
368+
try {
369+
const first = harness.registry.get(listAtom);
370+
expect(first).toHaveLength(1);
371+
setThread({ ...running, updatedAt: DateTime.add(v2Now, { seconds: 1 }), itemCount: 4 }, 2);
372+
expect(harness.registry.get(listAtom)).toBe(first);
373+
374+
setThread({ ...running, title: "Renamed" }, 3);
375+
expect(harness.registry.get(listAtom)).not.toBe(first);
376+
expect(harness.registry.get(listAtom)[0]?.title).toBe("Renamed");
377+
} finally {
378+
dispose();
379+
harness.registry.dispose();
380+
}
381+
});
347382
});

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

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -388,6 +388,20 @@ export function createEnvironmentThreadShellAtoms(input: {
388388
return previousThreadShells;
389389
}).pipe(Atom.withLabel("environment-thread-shell-list"));
390390

391+
// Lists that arrange threads read this, so a running thread's streaming
392+
// ticks reach only what reads its live shell.
393+
let previousQuietThreadShells: ReadonlyArray<EnvironmentThreadShell> = [];
394+
const quietThreadShellsAtom = Atom.make((get) => {
395+
const next: EnvironmentThreadShell[] = [];
396+
for (const ref of get(threadRefsAtom)) {
397+
const shell = get(quietThreadShellAtomFamily(threadKey(ref)));
398+
if (shell !== null) next.push(shell);
399+
}
400+
if (arrayElementsEqual(previousQuietThreadShells, next)) return previousQuietThreadShells;
401+
previousQuietThreadShells = next;
402+
return next;
403+
}).pipe(Atom.withLabel("environment-quiet-thread-shell-list"));
404+
391405
let previousNavigationShells: ReadonlyArray<EnvironmentThreadShell> = [];
392406
const navigationThreadShellsAtom = Atom.make((get) => {
393407
const next: EnvironmentThreadShell[] = [];
@@ -411,6 +425,7 @@ export function createEnvironmentThreadShellAtoms(input: {
411425
environmentThreadRefsByProjectAtom,
412426
threadRefsAtom,
413427
threadShellsAtom,
428+
quietThreadShellsAtom,
414429
navigationThreadShellsAtom,
415430
threadShellsForProjectRefsAtom: (refs: ReadonlyArray<ScopedProjectRef>) =>
416431
threadShellsForProjectRefsAtomFamily(projectRefCollectionKey(refs)),

0 commit comments

Comments
 (0)