TypeScript
perf(web): a streaming thread re-renders only its own sidebar row (#22730)
Julius Marminge5a0d1594e865
diff --git a/apps/web/src/components/Sidebar.tsx b/apps/web/src/components/Sidebar.tsxindex 1e77512583b..9447ce0ba66 100644--- a/apps/web/src/components/Sidebar.tsx+++ b/apps/web/src/components/Sidebar.tsx@@ -116,7 +116,7 @@ import {useAllEnvironmentProjectSnapshotsReady,useProjects,useServerConfigs,- useThreadShells,+ useQuietThreadShells,} from "../state/entities";import { environmentPresentations } from "../state/presentation";import { primaryServerKeybindingsAtom } from "../state/server";@@ -356,7 +356,7 @@ function useSidebarThreads(input: {}) {const { projectScopeKey, scopedProjectKeys, serverConfigs, optimisticDrop, routeThreadRef } =input;- const threads = useThreadShells();+ const threads = useQuietThreadShells();const routeRowThreadKey =routeThreadRef === null? nulldiff --git a/apps/web/src/components/sidebar/SidebarThreadRow.tsx b/apps/web/src/components/sidebar/SidebarThreadRow.tsxindex a73ca0725a9..96f1223810e 100644--- a/apps/web/src/components/sidebar/SidebarThreadRow.tsx+++ b/apps/web/src/components/sidebar/SidebarThreadRow.tsx@@ -96,6 +96,7 @@ import {ThreadSearchPullRequestNumber,} from "../ThreadStatusIndicators";import { resolveSnoozePresets, type SnoozePreset } from "../Sidebar.snooze";+import { useThreadShell } from "../../state/entities";import { ProjectFavicon, type ProjectFaviconProject } from "../ProjectFavicon";import { ThreadSearchMatchExcerpt } from "../ThreadSearchMatch";import { makeWorkspaceFileDropHandlers } from "../chat/workspaceFileDrop";@@ -463,8 +464,13 @@ export function SortableThreadRow(transition: contextDrag && isDragging ? "transform 150ms ease-out" : transition,isDragging: isDragging && !contextDrag,};+ // The sidebar arranges rows from quiet shells, which hold still while a+ // thread streams; reading the live shell here re-renders only this row.+ const thread =+ useThreadShell(scopeThreadRef(rowProps.thread.environmentId, rowProps.thread.id)) ??+ rowProps.thread;// Keyed by variant so a card/slim switch remounts the row.- return <SidebarThreadRow key={rowProps.variant} {...rowProps} sortable={bag} />;+ return <SidebarThreadRow key={rowProps.variant} {...rowProps} thread={thread} sortable={bag} />;}export type SidebarSweepAction = "settle" | "unsettle" | "unsnooze";const dropVerbBadge: Record<SidebarDropVerb, ReactNode> = {@@ -1508,7 +1514,8 @@ export const SidebarSearchResultRow = memo(function SidebarSearchResultRow(propsonSelect: () => void;onFileDropThreads: (threadRef: ScopedThreadRef, files: File[]) => void;}) {- const { thread } = props;+ const thread =+ useThreadShell(scopeThreadRef(props.thread.environmentId, props.thread.id)) ?? props.thread;const accessibility = resolveSidebarRowAccessibility({title: thread.title,statusLabel: null,diff --git a/apps/web/src/hooks/useSidebarProjectGroups.ts b/apps/web/src/hooks/useSidebarProjectGroups.tsindex 5011fe3953f..cb1b3b10132 100644--- a/apps/web/src/hooks/useSidebarProjectGroups.ts+++ b/apps/web/src/hooks/useSidebarProjectGroups.ts@@ -2,13 +2,15 @@ import { orderItemsByPreferredIds, sortSidebarV2ProjectGroups } from "../componeimport { getProjectOrderKey, selectProjectGroupingSettings } from "../logicalProject";import { buildSidebarProjectSnapshots } from "../sidebarProjectGrouping";import { useEnvironmentIdentities, usePrimaryEnvironmentId } from "../state/environments";-import { useProjects, useThreadShells } from "../state/entities";+import { useProjects, useQuietThreadShells } from "../state/entities";import { legacyProjectCwdPreferenceKey, useUiStateStore } from "../uiStateStore";import { useClientSettings } from "./useSettings";export function useSidebarProjectGroups() {const projects = useProjects();- const threads = useThreadShells();+ // Activity order lags a running thread's streaming ticks until its next+ // real change; reading every tick re-rendered the whole sidebar with it.+ const threads = useQuietThreadShells();const projectOrder = useUiStateStore((store) => store.projectOrder);const sortOrder = useClientSettings((s) => s.sidebarProjectSortOrder);const groupingSettings = useClientSettings(selectProjectGroupingSettings);diff --git a/apps/web/src/state/entities.ts b/apps/web/src/state/entities.tsindex 91fb46503b9..012bbb82ce2 100644--- a/apps/web/src/state/entities.ts+++ b/apps/web/src/state/entities.ts@@ -139,6 +139,11 @@ export function useQuietThreadShell(ref: ScopedThreadRef | null): EnvironmentThr);}+/** Every thread shell, held through streaming ticks; see quietThreadShellAtom. */+export function useQuietThreadShells(): ReadonlyArray<EnvironmentThreadShell> {+ return useAtomValue(environmentThreadShells.quietThreadShellsAtom);+}+export function useChildThreadInputs(ref: ScopedThreadRef | null) {return useAtomValue(ref === null ? EMPTY_THREAD_SHELLS_ATOM : environmentThreadShells.childThreadInputsAtom(ref),diff --git a/packages/client-runtime/src/state/threadShell.test.ts b/packages/client-runtime/src/state/threadShell.test.tsindex e8bae27f8f1..2dcc3233db3 100644--- a/packages/client-runtime/src/state/threadShell.test.ts+++ b/packages/client-runtime/src/state/threadShell.test.ts@@ -344,4 +344,39 @@ describe("v2 thread shell lists", () => {harness.registry.dispose();}});++ it("keeps the quiet shell list through streaming ticks", () => {+ const harness = makeHarness();+ const runId = RunId.make("streaming-list-run");+ const running: OrchestrationV2ThreadShell = {+ ...v2ThreadShell,+ latestRunId: runId,+ activeRunId: runId,+ status: "running",+ itemCount: 1,+ visibleItemCount: 1,+ };+ const setThread = (thread: OrchestrationV2ThreadShell, sequence: number) =>+ harness.registry.set(harness.snapshotAtom(environmentId), {+ ...v2ShellSnapshot,+ snapshotSequence: sequence,+ threads: [thread],+ });+ setThread(running, 1);+ const listAtom = harness.threads.quietThreadShellsAtom;+ const dispose = harness.registry.mount(listAtom);+ try {+ const first = harness.registry.get(listAtom);+ expect(first).toHaveLength(1);+ setThread({ ...running, updatedAt: DateTime.add(v2Now, { seconds: 1 }), itemCount: 4 }, 2);+ expect(harness.registry.get(listAtom)).toBe(first);++ setThread({ ...running, title: "Renamed" }, 3);+ expect(harness.registry.get(listAtom)).not.toBe(first);+ expect(harness.registry.get(listAtom)[0]?.title).toBe("Renamed");+ } finally {+ dispose();+ harness.registry.dispose();+ }+ });});diff --git a/packages/client-runtime/src/state/threadShell.ts b/packages/client-runtime/src/state/threadShell.tsindex a462638144b..8d61ff6ac56 100644--- a/packages/client-runtime/src/state/threadShell.ts+++ b/packages/client-runtime/src/state/threadShell.ts@@ -388,6 +388,20 @@ export function createEnvironmentThreadShellAtoms(input: {return previousThreadShells;}).pipe(Atom.withLabel("environment-thread-shell-list"));+ // Lists that arrange threads read this, so a running thread's streaming+ // ticks reach only what reads its live shell.+ let previousQuietThreadShells: ReadonlyArray<EnvironmentThreadShell> = [];+ const quietThreadShellsAtom = Atom.make((get) => {+ const next: EnvironmentThreadShell[] = [];+ for (const ref of get(threadRefsAtom)) {+ const shell = get(quietThreadShellAtomFamily(threadKey(ref)));+ if (shell !== null) next.push(shell);+ }+ if (arrayElementsEqual(previousQuietThreadShells, next)) return previousQuietThreadShells;+ previousQuietThreadShells = next;+ return next;+ }).pipe(Atom.withLabel("environment-quiet-thread-shell-list"));+let previousNavigationShells: ReadonlyArray<EnvironmentThreadShell> = [];const navigationThreadShellsAtom = Atom.make((get) => {const next: EnvironmentThreadShell[] = [];@@ -411,6 +425,7 @@ export function createEnvironmentThreadShellAtoms(input: {environmentThreadRefsByProjectAtom,threadRefsAtom,threadShellsAtom,+ quietThreadShellsAtom,navigationThreadShellsAtom,threadShellsForProjectRefsAtom: (refs: ReadonlyArray<ScopedProjectRef>) =>threadShellsForProjectRefsAtomFamily(projectRefCollectionKey(refs)),