From 361d76341ff7bc5c957a72c0f45051bcb2f1e4fd Mon Sep 17 00:00:00 2001 From: Neil Date: Mon, 16 Nov 2026 01:56:50 -0800 Subject: [PATCH 1/2] perf(sidebar): an open workspace menu no longer re-renders on unrelated store writes While open, the sidebar workspace menu subscribed to whole activity maps (tabs, PTYs, browser tabs, unified tabs, agent status, structured-chat sleep, delete state). Every agent-status, tab or PTY write anywhere re-rendered the whole menu under the pointer: about 200 components per write against 10 with it closed. It now selects only what it shows: the ids, among the context and lineage workspaces, that are sleepable or being deleted, compared shallowly. Lineage reads the catalog alone. Measured on the e2e build with an 870-workspace accumulated profile, menu open, one agent-status write: 183-205 -> 10 components re-rendered, 3.1-3.2 -> 2.3 ms per write (same as closed). Regression test: 20 unrelated writes re-render the open menu 0 times (was 20); it still updates when its own workspace becomes sleepable. --- ...worktree-context-menu-model.churn.test.tsx | 74 ++++++++++++ .../use-worktree-context-menu-model.tsx | 109 +++++------------- .../workspace-lineage-menu-actions.ts | 95 +++++++-------- .../worktree-context-menu-policy.ts | 7 -- 4 files changed, 153 insertions(+), 132 deletions(-) create mode 100644 src/renderer/src/components/sidebar/workspace-actions/context-menu/use-worktree-context-menu-model.churn.test.tsx diff --git a/src/renderer/src/components/sidebar/workspace-actions/context-menu/use-worktree-context-menu-model.churn.test.tsx b/src/renderer/src/components/sidebar/workspace-actions/context-menu/use-worktree-context-menu-model.churn.test.tsx new file mode 100644 index 00000000000..9c68af9ae48 --- /dev/null +++ b/src/renderer/src/components/sidebar/workspace-actions/context-menu/use-worktree-context-menu-model.churn.test.tsx @@ -0,0 +1,74 @@ +// @vitest-environment happy-dom + +import { act, cleanup, renderHook } from '@testing-library/react' +import { afterEach, describe, expect, it } from 'vitest' +import { useAppStore } from '@/store' +import { makeWorktree } from '@/store/slices/worktrees/worktree.test-fixture' +import { TEST_REPO } from '@/store/repos/repo.test-fixture' +import { useWorktreeContextMenuModel } from './use-worktree-context-menu-model' + +afterEach(() => { + cleanup() + useAppStore.setState(useAppStore.getInitialState(), true) +}) + +const worktree = makeWorktree({ id: 'repo1::/tmp/wt', repoId: TEST_REPO.id }) +const other = makeWorktree({ + id: 'repo1::/tmp/other', + repoId: TEST_REPO.id, + path: '/tmp/other' +}) + +function renderOpenMenu() { + useAppStore.setState({ + repos: [TEST_REPO], + worktreesByRepo: { [TEST_REPO.id]: [worktree, other] }, + tabsByWorktree: { [worktree.id]: [{ id: 'tab-1' }] } as never, + ptyIdsByTabId: {} + }) + let renders = 0 + const hook = renderHook(() => { + renders += 1 + return useWorktreeContextMenuModel({ worktree, children: null }) + }) + act(() => hook.result.current.setMenuOpenState(true)) + return { hook, renders: () => renders } +} + +// An open menu used to subscribe to whole activity maps, so every agent status, tab or PTY write +// anywhere re-rendered the whole menu (about 200 components) while the pointer moved over it. +describe('an open workspace menu ignores store writes that change nothing it shows', () => { + it('another workspace’s agent status, tabs and PTYs do not re-render it', () => { + const { renders } = renderOpenMenu() + const before = renders() + for (let i = 0; i < 20; i += 1) { + act(() => { + const state = useAppStore.getState() + useAppStore.setState({ + agentStatusByPaneKey: { + ...state.agentStatusByPaneKey, + [`pane-${i}`]: { updatedAt: i } as never + }, + tabsByWorktree: { + ...state.tabsByWorktree, + [other.id]: [{ id: `other-${i}` }] as never + }, + ptyIdsByTabId: { + ...state.ptyIdsByTabId, + [`other-${i}`]: [`pty-${i}`] + }, + unifiedTabsByWorktree: { ...state.unifiedTabsByWorktree }, + deleteStateByWorktreeId: { ...state.deleteStateByWorktreeId } + }) + }) + } + expect(renders()).toBe(before) + }) + + it('still updates live when this workspace becomes sleepable', () => { + const { hook } = renderOpenMenu() + expect(hook.result.current.sleepableWorktrees).toEqual([]) + act(() => useAppStore.setState({ ptyIdsByTabId: { 'tab-1': ['pty-1'] } })) + expect(hook.result.current.sleepableWorktrees.map((item) => item.id)).toEqual([worktree.id]) + }) +}) diff --git a/src/renderer/src/components/sidebar/workspace-actions/context-menu/use-worktree-context-menu-model.tsx b/src/renderer/src/components/sidebar/workspace-actions/context-menu/use-worktree-context-menu-model.tsx index fc090f95d42..93d7a090de8 100644 --- a/src/renderer/src/components/sidebar/workspace-actions/context-menu/use-worktree-context-menu-model.tsx +++ b/src/renderer/src/components/sidebar/workspace-actions/context-menu/use-worktree-context-menu-model.tsx @@ -1,4 +1,3 @@ -import { selectAwakeBrowserTabsByWorktree } from '@/components/sidebar/workspace-actions/sleep/browser-tab-sleep' import { useIsPairedCloudPrimaryRow } from '@/lib/orca-cloud/cloud-host-rows' import { useCallback, useEffect, useMemo, useRef, useState } from 'react' import { useAppStore } from '@/store/index' @@ -7,20 +6,12 @@ import type { Worktree } from '../../../../../../shared/worktree/types' import { useContextWorkspaceStatuses } from '../../workspace-status/presentation/use-workspace-board-statuses' import { useMenuScopedWorktreeCatalog } from './use-menu-scoped-worktree-catalog' import { useMenuLineageParent } from './use-menu-lineage-parent' -import { - hasSleepableWorkspaceActivity, - useWorkspaceLineageMenuActions -} from './workspace-lineage-menu-actions' +import { useWorkspaceMenuActivity } from './workspace-lineage-menu-actions' +import { getWorkspaceDeleteLineage } from '../deletion/workspace-delete-lineage' import { parseWorkspaceKey } from '../../../../../../shared/workspace-scope' import { getDeleteStateForWorktreeHost } from '../deletion/worktree-delete-state-host-match' import { - EMPTY_AGENT_STATUS_BY_PANE_KEY, - EMPTY_BROWSER_TABS_BY_WORKTREE, - EMPTY_DELETE_STATE_BY_WORKTREE_ID, - EMPTY_STRUCTURED_CHAT_SLEPT_AT, - EMPTY_PTY_IDS_BY_TAB_ID, - EMPTY_TABS_BY_WORKTREE, - EMPTY_UNIFIED_TABS_BY_WORKTREE, + EMPTY_WORKTREES, EMPTY_WORKSPACE_LINEAGE_BY_CHILD_KEY, EMPTY_WORKTREE_LINEAGE_BY_ID, getSharedWorkspaceStatus, @@ -102,31 +93,6 @@ export function useWorktreeContextMenuModel({ ) ) const updateWorktreeLineage = useAppStore((s) => s.updateWorktreeLineage) - const tabsByWorktree = useAppStore((s) => - selectMenuScopedMap(menuOpen, s.tabsByWorktree, EMPTY_TABS_BY_WORKTREE) - ) - const ptyIdsByTabId = useAppStore((s) => - selectMenuScopedMap(menuOpen, s.ptyIdsByTabId, EMPTY_PTY_IDS_BY_TAB_ID) - ) - const browserTabsByWorktree = useAppStore((s) => - selectMenuScopedMap( - menuOpen, - selectAwakeBrowserTabsByWorktree(s), - EMPTY_BROWSER_TABS_BY_WORKTREE - ) - ) - const unifiedTabsByWorktree = useAppStore((s) => - selectMenuScopedMap(menuOpen, s.unifiedTabsByWorktree, EMPTY_UNIFIED_TABS_BY_WORKTREE) - ) - const agentStatusByPaneKey = useAppStore((s) => - selectMenuScopedMap(menuOpen, s.agentStatusByPaneKey, EMPTY_AGENT_STATUS_BY_PANE_KEY) - ) - const structuredChatSleptAtByPaneKey = useAppStore((s) => - selectMenuScopedMap(menuOpen, s.structuredChatSleptAtByPaneKey, EMPTY_STRUCTURED_CHAT_SLEPT_AT) - ) - const deleteStateByWorktreeId = useAppStore((s) => - selectMenuScopedMap(menuOpen, s.deleteStateByWorktreeId, EMPTY_DELETE_STATE_BY_WORKTREE_ID) - ) const scopeRef = useRef(null) const contextMenuOpenedAtRef = useRef(null) const activeContextWorktrees = menuOpen ? contextWorktrees : effectiveSelectedWorktrees @@ -136,54 +102,42 @@ export function useWorktreeContextMenuModel({ const workspaceScope = parseWorkspaceKey(worktree.id) const folderWorkspaceId = workspaceScope?.type === 'folder' ? workspaceScope.folderWorkspaceId : null - const sleepActivity = useMemo( - () => ({ - tabsByWorktree, - ptyIdsByTabId, - browserTabsByWorktree, - unifiedTabsByWorktree, - agentStatusByPaneKey, - structuredChatSleptAtByPaneKey - }), - [ - agentStatusByPaneKey, - browserTabsByWorktree, - structuredChatSleptAtByPaneKey, - ptyIdsByTabId, - tabsByWorktree, - unifiedTabsByWorktree - ] - ) // A paired cloud workspace can be running with nothing open here: its Sleep reaches the sandbox. const pairedCloudSleepable = useIsPairedCloudPrimaryRow(worktree) && !isMultiContext + // Lineage reads only the catalog; per-workspace activity comes from narrow id selectors below. + const lineageTargets = useMemo( + () => + menuOpen && !isMultiContext + ? [ + worktree, + ...getWorkspaceDeleteLineage(worktree, allWorktrees, worktreeLineageById).descendants + ] + : EMPTY_WORKTREES, + [allWorktrees, isMultiContext, menuOpen, worktree, worktreeLineageById] + ) + const activityCandidates = useMemo( + () => [ + ...activeContextWorktrees, + ...lineageTargets.filter((target) => !activeContextWorktrees.includes(target)) + ], + [activeContextWorktrees, lineageTargets] + ) + const { sleepableIds, deletingIds } = useWorkspaceMenuActivity(menuOpen, activityCandidates) const sleepableWorktrees = useMemo( () => activeContextWorktrees.filter( (item) => - hasSleepableWorkspaceActivity(item.id, sleepActivity) || - (pairedCloudSleepable && item.id === worktree.id) + sleepableIds.includes(item.id) || (pairedCloudSleepable && item.id === worktree.id) ), - [activeContextWorktrees, sleepActivity, pairedCloudSleepable, worktree.id] + [activeContextWorktrees, sleepableIds, pairedCloudSleepable, worktree.id] ) - const lineageMenuActions = useWorkspaceLineageMenuActions({ - enabled: menuOpen && !isMultiContext, - parent: worktree, - worktrees: allWorktrees, - lineageById: worktreeLineageById, - activity: sleepActivity - }) - const lineageDescendantCount = lineageMenuActions.descendants.length - const subtreeSleepableWorktrees = lineageMenuActions.sleepableTargets - const deletingContext = useMemo( - () => - activeContextWorktrees.some( - (item) => getDeleteStateForWorktreeHost(item, deleteStateByWorktreeId)?.isDeleting - ), - [activeContextWorktrees, deleteStateByWorktreeId] - ) - const deletingSubtree = lineageMenuActions.targets.some( - (item) => getDeleteStateForWorktreeHost(item, deleteStateByWorktreeId)?.isDeleting + const lineageDescendantCount = Math.max(0, lineageTargets.length - 1) + const subtreeSleepableWorktrees = useMemo( + () => lineageTargets.filter((target) => sleepableIds.includes(target.id)), + [lineageTargets, sleepableIds] ) + const deletingContext = activeContextWorktrees.some((item) => deletingIds.includes(item.id)) + const deletingSubtree = lineageTargets.some((item) => deletingIds.includes(item.id)) const contextDeletePending = isMultiContext ? deletingContext : deletingSubtree const contextWorkspaceStatus = useMemo( () => getSharedWorkspaceStatus(activeContextWorktrees, statusesForWorktree), @@ -343,7 +297,6 @@ export function useWorktreeContextMenuModel({ ...commands, activeContextWorktrees, batchDeleteWorktrees, - browserTabsByWorktree, children, contentClassName, contextDeletePending, @@ -373,7 +326,6 @@ export function useWorktreeContextMenuModel({ parentPicker, parentPickerOpen, projectGroups, - ptyIdsByTabId, removesProject, repo, scopeRef, @@ -385,7 +337,6 @@ export function useWorktreeContextMenuModel({ sleepableWorktrees, subtreeSleepableWorktrees, suppressOpeningPointerEvent, - tabsByWorktree, validParentWorktreeId, worktree, workspaceStatuses diff --git a/src/renderer/src/components/sidebar/workspace-actions/context-menu/workspace-lineage-menu-actions.ts b/src/renderer/src/components/sidebar/workspace-actions/context-menu/workspace-lineage-menu-actions.ts index 645697c35c3..09a6e9ce6e7 100644 --- a/src/renderer/src/components/sidebar/workspace-actions/context-menu/workspace-lineage-menu-actions.ts +++ b/src/renderer/src/components/sidebar/workspace-actions/context-menu/workspace-lineage-menu-actions.ts @@ -1,4 +1,8 @@ -import { useMemo } from 'react' +import { useShallow } from 'zustand/react/shallow' +import { useAppStore } from '@/store' +import type { AppState } from '@/store/types' +import { selectAwakeBrowserTabsByWorktree } from '@/components/sidebar/workspace-actions/sleep/browser-tab-sleep' +import { getDeleteStateForWorktreeHost } from '../deletion/worktree-delete-state-host-match' import { tabHasLivePty } from '../../../../../../shared/tab-has-live-pty' import type { WorktreeLineage } from '../../../../../../shared/worktree/lineage-types' import type { Worktree } from '../../../../../../shared/worktree/types' @@ -61,52 +65,51 @@ export function getWorkspaceLineageMenuActions(args: { } } -const EMPTY_LINEAGE_MENU_ACTIONS: WorkspaceLineageMenuActions = { - descendants: [], - targets: [], - sleepableTargets: [] +const EMPTY_IDS: readonly string[] = [] + +function selectWorkspaceActivity(state: AppState): WorkspaceActivityMaps { + return { + tabsByWorktree: state.tabsByWorktree, + ptyIdsByTabId: state.ptyIdsByTabId, + browserTabsByWorktree: selectAwakeBrowserTabsByWorktree(state), + unifiedTabsByWorktree: state.unifiedTabsByWorktree, + agentStatusByPaneKey: state.agentStatusByPaneKey, + structuredChatSleptAtByPaneKey: state.structuredChatSleptAtByPaneKey + } } -export function useWorkspaceLineageMenuActions( - args: Parameters[0] & { enabled: boolean } -): WorkspaceLineageMenuActions { - const { enabled, parent, worktrees, lineageById, activity } = args - const { - tabsByWorktree, - ptyIdsByTabId, - browserTabsByWorktree, - unifiedTabsByWorktree, - agentStatusByPaneKey, - structuredChatSleptAtByPaneKey - } = activity - return useMemo( - () => - enabled - ? getWorkspaceLineageMenuActions({ - parent, - worktrees, - lineageById, - activity: { - tabsByWorktree, - ptyIdsByTabId, - browserTabsByWorktree, - unifiedTabsByWorktree, - agentStatusByPaneKey, - structuredChatSleptAtByPaneKey - } - }) - : EMPTY_LINEAGE_MENU_ACTIONS, - [ - agentStatusByPaneKey, - browserTabsByWorktree, - enabled, - structuredChatSleptAtByPaneKey, - lineageById, - parent, - ptyIdsByTabId, - tabsByWorktree, - unifiedTabsByWorktree, - worktrees - ] +/** + * Which of `worktrees` have something Sleep would close, and which are being deleted, as id lists + * that keep their identity while unchanged. An open menu must not subscribe to the whole activity + * and delete maps: agents, tabs and PTYs anywhere write them many times a second, and each write + * re-rendered the whole menu under the pointer. Closed menus select a shared empty list. + */ +export function useWorkspaceMenuActivity( + enabled: boolean, + worktrees: readonly Worktree[] +): { sleepableIds: readonly string[]; deletingIds: readonly string[] } { + const sleepableIds = useAppStore( + useShallow((state) => { + if (!enabled) { + return EMPTY_IDS + } + const activity = selectWorkspaceActivity(state) + return worktrees + .filter((item) => hasSleepableWorkspaceActivity(item.id, activity)) + .map((item) => item.id) + }) ) + const deletingIds = useAppStore( + useShallow((state) => + enabled + ? worktrees + .filter( + (item) => + getDeleteStateForWorktreeHost(item, state.deleteStateByWorktreeId)?.isDeleting + ) + .map((item) => item.id) + : EMPTY_IDS + ) + ) + return { sleepableIds, deletingIds } } diff --git a/src/renderer/src/components/sidebar/workspace-actions/context-menu/worktree-context-menu-policy.ts b/src/renderer/src/components/sidebar/workspace-actions/context-menu/worktree-context-menu-policy.ts index 17fef0348bc..b0316370e6b 100644 --- a/src/renderer/src/components/sidebar/workspace-actions/context-menu/worktree-context-menu-policy.ts +++ b/src/renderer/src/components/sidebar/workspace-actions/context-menu/worktree-context-menu-policy.ts @@ -25,13 +25,6 @@ const DELETE_POSITION_RESTORE_STABLE_FRAMES = 6 export const PARENT_PICKER_EXIT_ANIMATION_MS = 200 // Closed menus reuse empty identities so teardown map churn cannot wake their subscriptions. -export const EMPTY_TABS_BY_WORKTREE: AppState['tabsByWorktree'] = {} -export const EMPTY_PTY_IDS_BY_TAB_ID: AppState['ptyIdsByTabId'] = {} -export const EMPTY_BROWSER_TABS_BY_WORKTREE: AppState['browserTabsByWorktree'] = {} -export const EMPTY_UNIFIED_TABS_BY_WORKTREE: AppState['unifiedTabsByWorktree'] = {} -export const EMPTY_AGENT_STATUS_BY_PANE_KEY: AppState['agentStatusByPaneKey'] = {} -export const EMPTY_STRUCTURED_CHAT_SLEPT_AT: AppState['structuredChatSleptAtByPaneKey'] = {} -export const EMPTY_DELETE_STATE_BY_WORKTREE_ID: AppState['deleteStateByWorktreeId'] = {} export const EMPTY_WORKTREE_LINEAGE_BY_ID: AppState['worktreeLineageById'] = {} export const EMPTY_WORKSPACE_LINEAGE_BY_CHILD_KEY: AppState['workspaceLineageByChildKey'] = {} export const EMPTY_CROSS_STORE_LINEAGE_BY_STORE_KEY: AppState['crossStoreLineageByStoreKey'] = {} From 9a5a5c27789a8882e94d675ab77fd57cad4cc304 Mon Sep 17 00:00:00 2001 From: Neil Date: Mon, 16 Nov 2026 02:01:28 -0800 Subject: [PATCH 2/2] fix(sidebar): churn test without type casts; layout-writer ratchet clean --- ...worktree-context-menu-model.churn.test.tsx | 21 ++++++++++--------- .../workspace-lineage-menu-actions.ts | 3 ++- 2 files changed, 13 insertions(+), 11 deletions(-) diff --git a/src/renderer/src/components/sidebar/workspace-actions/context-menu/use-worktree-context-menu-model.churn.test.tsx b/src/renderer/src/components/sidebar/workspace-actions/context-menu/use-worktree-context-menu-model.churn.test.tsx index 9c68af9ae48..1f891b7e431 100644 --- a/src/renderer/src/components/sidebar/workspace-actions/context-menu/use-worktree-context-menu-model.churn.test.tsx +++ b/src/renderer/src/components/sidebar/workspace-actions/context-menu/use-worktree-context-menu-model.churn.test.tsx @@ -5,6 +5,7 @@ import { afterEach, describe, expect, it } from 'vitest' import { useAppStore } from '@/store' import { makeWorktree } from '@/store/slices/worktrees/worktree.test-fixture' import { TEST_REPO } from '@/store/repos/repo.test-fixture' +import { makeTab } from '@/store/terminals/terminal-tab.test-fixture' import { useWorktreeContextMenuModel } from './use-worktree-context-menu-model' afterEach(() => { @@ -23,7 +24,7 @@ function renderOpenMenu() { useAppStore.setState({ repos: [TEST_REPO], worktreesByRepo: { [TEST_REPO.id]: [worktree, other] }, - tabsByWorktree: { [worktree.id]: [{ id: 'tab-1' }] } as never, + tabsByWorktree: { [worktree.id]: [makeTab({ id: 'tab-1', worktreeId: worktree.id })] }, ptyIdsByTabId: {} }) let renders = 0 @@ -44,19 +45,19 @@ describe('an open workspace menu ignores store writes that change nothing it sho for (let i = 0; i < 20; i += 1) { act(() => { const state = useAppStore.getState() + state.setAgentStatus( + `pane-${i}`, + { state: 'working', prompt: `prompt ${i}`, agentType: 'codex' }, + 'codex', + { updatedAt: i, stateStartedAt: i }, + { tabId: `other-${i}`, worktreeId: other.id } + ) useAppStore.setState({ - agentStatusByPaneKey: { - ...state.agentStatusByPaneKey, - [`pane-${i}`]: { updatedAt: i } as never - }, tabsByWorktree: { ...state.tabsByWorktree, - [other.id]: [{ id: `other-${i}` }] as never - }, - ptyIdsByTabId: { - ...state.ptyIdsByTabId, - [`other-${i}`]: [`pty-${i}`] + [other.id]: [makeTab({ id: `other-${i}`, worktreeId: other.id })] }, + ptyIdsByTabId: { ...state.ptyIdsByTabId, [`other-${i}`]: [`pty-${i}`] }, unifiedTabsByWorktree: { ...state.unifiedTabsByWorktree }, deleteStateByWorktreeId: { ...state.deleteStateByWorktreeId } }) diff --git a/src/renderer/src/components/sidebar/workspace-actions/context-menu/workspace-lineage-menu-actions.ts b/src/renderer/src/components/sidebar/workspace-actions/context-menu/workspace-lineage-menu-actions.ts index 09a6e9ce6e7..6bbab62bc76 100644 --- a/src/renderer/src/components/sidebar/workspace-actions/context-menu/workspace-lineage-menu-actions.ts +++ b/src/renderer/src/components/sidebar/workspace-actions/context-menu/workspace-lineage-menu-actions.ts @@ -68,10 +68,11 @@ export function getWorkspaceLineageMenuActions(args: { const EMPTY_IDS: readonly string[] = [] function selectWorkspaceActivity(state: AppState): WorkspaceActivityMaps { + const browserTabsByWorktree = selectAwakeBrowserTabsByWorktree(state) return { tabsByWorktree: state.tabsByWorktree, ptyIdsByTabId: state.ptyIdsByTabId, - browserTabsByWorktree: selectAwakeBrowserTabsByWorktree(state), + browserTabsByWorktree, unifiedTabsByWorktree: state.unifiedTabsByWorktree, agentStatusByPaneKey: state.agentStatusByPaneKey, structuredChatSleptAtByPaneKey: state.structuredChatSleptAtByPaneKey