The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
perf(desktop): stop a settling turn from repainting the whole sidebar
Brooklyn Nicholson615a435b7aa7
diff --git a/apps/desktop/src/app/chat/sidebar/index.tsx b/apps/desktop/src/app/chat/sidebar/index.tsxindex 46a150596f7..33090ed4886 100644--- a/apps/desktop/src/app/chat/sidebar/index.tsx+++ b/apps/desktop/src/app/chat/sidebar/index.tsx@@ -303,7 +303,6 @@ export function ChatSidebar({const messagingTruncated = useStore($messagingTruncated)const sessionsLoading = useStore($sessionsLoading)const sessionProfilesTruncated = useStore($sessionProfilesTruncated)- const workingSessionIds = useStore($workingSessionIds)const profiles = useStore($profiles)const profileScope = useStore($profileScope)// Only surface the profile switcher when more than one profile exists, so@@ -757,20 +756,30 @@ export function ChatSidebar({// session settles (its turn finished) or the window refocuses (an external// terminal may have changed things) — only while a project is entered, and// only the cheap per-repo `git worktree list`, never the heavy tree scan.- const prevWorkingIdsRef = useRef<readonly string[]>(workingSessionIds)-- // eslint-disable-next-line no-restricted-syntax -- legitimate non-atom ref write (see eslint rule comment)+ //+ // Listened to rather than rendered from: a settling turn is a side effect,+ // and reading it with `useStore` repainted this whole component — every+ // section, every row — on each status edge, to run an effect that touches no+ // markup. The rows subscribe to their own status, so nothing above them needs+ // to re-render for one of them to change color.useEffect(() => {- const prev = prevWorkingIdsRef.current- prevWorkingIdsRef.current = workingSessionIds+ if (!inEnteredProject) {+ return+ }- // A session leaving the working set means its turn just completed.- const aTurnSettled = prev.some(id => !workingSessionIds.includes(id))+ let previous = $workingSessionIds.get()- if (inEnteredProject && aTurnSettled) {- refreshWorktrees()- }- }, [workingSessionIds, inEnteredProject])+ return $workingSessionIds.listen(working => {+ // A session leaving the working set means its turn just completed.+ const aTurnSettled = previous.some(id => !working.includes(id))++ previous = working++ if (aTurnSettled) {+ refreshWorktrees()+ }+ })+ }, [inEnteredProject])useEffect(() => {if (!inEnteredProject) {diff --git a/apps/desktop/src/app/chat/sidebar/session-row.test.tsx b/apps/desktop/src/app/chat/sidebar/session-row.test.tsxindex 6ce6304546f..f3c91c5411e 100644--- a/apps/desktop/src/app/chat/sidebar/session-row.test.tsx+++ b/apps/desktop/src/app/chat/sidebar/session-row.test.tsx@@ -1,4 +1,4 @@-import { cleanup, render, screen } from '@testing-library/react'+import { act, cleanup, render, screen } from '@testing-library/react'import { atom } from 'nanostores'import type * as React from 'react'import { afterEach, describe, expect, it, vi } from 'vitest'@@ -46,11 +46,15 @@ vi.mock('@/app/chat/session-drag', () => ({ startSessionDrag: vi.fn() }))// regression in its ref/prop forwarding fails here again.// Only `sessionTitle` is overridden (makeSession fakes a bare `title` the real// one wouldn't read); the rest of the module is genuine so the arc test can-// build session state with the same factory the app uses.+// build session state with the same factory the app uses. It is a spy because+// the row calls it exactly once per render, which is how the isolation test+// below counts repaints.+const sessionTitle = vi.fn((s: SessionInfo) => (s as unknown as { title: string }).title)+vi.mock('@/lib/chat-runtime', async importOriginal => {const actual = await importOriginal<typeof ChatRuntime>()- return { ...actual, sessionTitle: (s: SessionInfo) => (s as unknown as { title: string }).title }+ return { ...actual, sessionTitle: (s: SessionInfo) => sessionTitle(s) }})vi.mock('@/lib/haptics', () => ({ triggerHaptic: vi.fn() }))vi.mock('@/lib/session-source', () => ({@@ -171,6 +175,36 @@ describe('SidebarSessionRow running arc', () => {expect(arc(container)).toBeTruthy()})++ // The row owns its status subscription so a turn starting repaints that row+ // and nothing else — not its siblings, and not the list around them. Rows+ // render once per fiber, so counting `sessionTitle` counts repaints.+ it('repaints only the session whose turn started', () => {+ render(+ <>+ {[makeSession({ id: 's1', title: 'One' }), makeSession({ id: 's2', title: 'Two' })].map(session => (+ <SidebarSessionRow+ isPinned={false}+ isSelected={false}+ key={session.id}+ onArchive={noop}+ onDelete={noop}+ onPin={noop}+ onResume={noop}+ session={session}+ />+ ))}+ </>+ )+ sessionTitle.mockClear()++ act(() => {+ publishSessionState('rt1', { ...createClientSessionState('s1'), busy: true })+ })++ expect(sessionTitle).toHaveBeenCalledTimes(1)+ expect(sessionTitle).toHaveBeenCalledWith(expect.objectContaining({ id: 's1' }))+ })})describe('SidebarSessionRow', () => {