From 7de40ffbd8b1e087fb432a8dd2c0ccac0d658d1f Mon Sep 17 00:00:00 2001 From: Brennan Benson <79079362+brennanb2025@users.noreply.github.com> Date: Mon, 3 Aug 2026 14:41:22 -0700 Subject: [PATCH] fix(terminal): stop keyboard effect re-registering on every pane render (STA-3291) (#12348) createExpandCollapseActions minted five fresh closures per TerminalPane render; four are deps of useTerminalKeyboardShortcuts, so its seven window listeners tore down and re-registered on every render and all effect-owned IME chord/modifier state silently reset. Memoize the actions via useExpandCollapseActions and pin registration stability with a render-churn regression test. Fixes #12269 --- .../components/terminal-pane/TerminalPane.tsx | 6 +- .../expand-collapse-render-stability.test.ts | 126 ++++++++++++++++++ .../terminal-pane/expand-collapse.ts | 42 ++++++ 3 files changed, 171 insertions(+), 3 deletions(-) create mode 100644 src/renderer/src/components/terminal-pane/expand-collapse-render-stability.test.ts diff --git a/src/renderer/src/components/terminal-pane/TerminalPane.tsx b/src/renderer/src/components/terminal-pane/TerminalPane.tsx index cd32d81f6..9666ae98e 100644 --- a/src/renderer/src/components/terminal-pane/TerminalPane.tsx +++ b/src/renderer/src/components/terminal-pane/TerminalPane.tsx @@ -56,8 +56,8 @@ import type { TerminalKittyKeyboardModeTracker } from '../../../../shared/termin import { applyExpandedLayoutTo, cancelPendingPaneSizeRefreshFrames, - createExpandCollapseActions, - restoreExpandedLayoutFrom + restoreExpandedLayoutFrom, + useExpandCollapseActions } from './expand-collapse' import { useTerminalKeyboardShortcuts, type SearchState } from './keyboard-handlers' import type { MacOptionAsAlt } from './terminal-shortcut-policy' @@ -1250,7 +1250,7 @@ function TerminalPane( refreshPaneSizes, syncExpandedLayout, toggleExpandPane - } = createExpandCollapseActions({ + } = useExpandCollapseActions({ expandedPaneIdRef, expandedStyleSnapshotRef, containerRef, diff --git a/src/renderer/src/components/terminal-pane/expand-collapse-render-stability.test.ts b/src/renderer/src/components/terminal-pane/expand-collapse-render-stability.test.ts new file mode 100644 index 000000000..5f4ed9100 --- /dev/null +++ b/src/renderer/src/components/terminal-pane/expand-collapse-render-stability.test.ts @@ -0,0 +1,126 @@ +// @vitest-environment happy-dom +import { cleanup, renderHook } from '@testing-library/react' +import { afterEach, describe, expect, it, vi } from 'vitest' +import type { PaneManager } from '@/lib/pane-manager/pane-manager' +import type { PtyTransport } from './pty-transport' +import { useExpandCollapseActions } from './expand-collapse' +import { useTerminalKeyboardShortcuts } from './keyboard-handlers' + +type ExpandCollapseHookState = Parameters[0] +type KeyboardHandlersDeps = Parameters[0] + +// Field identities are stable across renders (refs, setState, store actions, +// useCallback); TerminalPane rebuilds only the wrapping object literal. +function createStableFields(): Omit { + return { + expandedPaneIdRef: { current: null }, + expandedStyleSnapshotRef: { current: new Map() }, + containerRef: { current: null }, + managerRef: { current: null }, + setExpandedPaneId: vi.fn(), + setTabPaneExpanded: vi.fn(), + pendingPaneSizeRefreshFrameIdsRef: { current: [] }, + persistLayoutSnapshot: vi.fn() + } +} + +afterEach(() => { + cleanup() + vi.restoreAllMocks() +}) + +describe('useExpandCollapseActions render stability', () => { + it('keeps all five action identities across rerenders that rebuild the state object', () => { + const fields = createStableFields() + const hook = renderHook(() => useExpandCollapseActions({ ...fields, tabId: 'tab-1' })) + const first = hook.result.current + + hook.rerender() + hook.rerender() + + const actionNames = Object.keys(first) as (keyof typeof first)[] + expect(actionNames).toHaveLength(5) + for (const name of actionNames) { + expect(Object.is(hook.result.current[name], first[name])).toBe(true) + } + }) + + it('remints actions when tabId changes', () => { + const fields = createStableFields() + const hook = renderHook(({ tabId }) => useExpandCollapseActions({ ...fields, tabId }), { + initialProps: { tabId: 'tab-1' } + }) + const first = hook.result.current + + hook.rerender({ tabId: 'tab-2' }) + + expect(Object.is(hook.result.current.setExpandedPane, first.setExpandedPane)).toBe(false) + }) +}) + +describe('terminal keyboard effect registration stability', () => { + it('registers window listeners once across rerenders with TerminalPane-shaped wiring', () => { + const scope = document.createElement('div') + document.body.append(scope) + const pane = { + id: 1, + leafId: '00000000-0000-4000-8000-000000000001', + terminal: { element: scope, focus: vi.fn(), getSelection: vi.fn(() => '') } + } + const manager = { + getActivePane: () => pane, + getPanes: () => [pane] + } as unknown as PaneManager + const transport = { getPtyId: () => 'pty-1', sendInput: vi.fn(() => true) } + const fields = createStableFields() + const stableDeps = { + tabId: 'tab-1', + worktreeId: 'worktree-1', + isActive: true, + keyboardScopeRef: { current: scope }, + managerRef: { current: manager }, + paneTransportsRef: { + current: new Map([[pane.id, transport as unknown as PtyTransport]]) + }, + panePtyBindingsRef: { current: new Map() }, + paneCwdRef: { current: new Map() }, + fallbackCwd: '', + expandedPaneIdRef: { current: null }, + setSearchOpen: vi.fn(), + onSearchSelectedText: vi.fn(), + onRequestClosePane: vi.fn(), + onClearPaneScrollback: vi.fn(), + onSetTitle: vi.fn(), + onClearPaneTitle: vi.fn(), + searchOpenRef: { current: false }, + searchStateRef: { current: { query: '', caseSensitive: false, regex: false } }, + macOptionAsAltRef: { current: 'false' as const } + } + + const addSpy = vi.spyOn(window, 'addEventListener') + const countBeforeinputAdds = (): number => + addSpy.mock.calls.filter(([type]) => type === 'beforeinput').length + + const hook = renderHook(() => { + // Same wiring shape as TerminalPane: both dep objects are rebuilt every render. + const actions = useExpandCollapseActions({ ...fields, tabId: stableDeps.tabId }) + useTerminalKeyboardShortcuts({ + ...stableDeps, + setExpandedPane: actions.setExpandedPane, + restoreExpandedLayout: actions.restoreExpandedLayout, + refreshPaneSizes: actions.refreshPaneSizes, + persistLayoutSnapshot: fields.persistLayoutSnapshot, + toggleExpandPane: actions.toggleExpandPane + } as KeyboardHandlersDeps) + }) + expect(countBeforeinputAdds()).toBe(1) + + for (let render = 0; render < 10; render++) { + hook.rerender() + } + + expect(countBeforeinputAdds()).toBe(1) + hook.unmount() + scope.remove() + }) +}) diff --git a/src/renderer/src/components/terminal-pane/expand-collapse.ts b/src/renderer/src/components/terminal-pane/expand-collapse.ts index c2d7b6966..1b21fbd1d 100644 --- a/src/renderer/src/components/terminal-pane/expand-collapse.ts +++ b/src/renderer/src/components/terminal-pane/expand-collapse.ts @@ -1,3 +1,4 @@ +import { useMemo } from 'react' import type { PaneManager } from '@/lib/pane-manager/pane-manager' import { safeFit } from '@/lib/pane-manager/pane-tree-ops' @@ -205,3 +206,44 @@ export function createExpandCollapseActions(state: ExpandCollapseState) { toggleExpandPane } } + +// Why: four of these actions are deps of the terminal keyboard effect; unstable +// identities would tear down and re-register its window listeners every render. +export function useExpandCollapseActions(state: ExpandCollapseState) { + const { + expandedPaneIdRef, + expandedStyleSnapshotRef, + containerRef, + managerRef, + setExpandedPaneId, + setTabPaneExpanded, + pendingPaneSizeRefreshFrameIdsRef, + tabId, + persistLayoutSnapshot + } = state + return useMemo( + () => + createExpandCollapseActions({ + expandedPaneIdRef, + expandedStyleSnapshotRef, + containerRef, + managerRef, + setExpandedPaneId, + setTabPaneExpanded, + pendingPaneSizeRefreshFrameIdsRef, + tabId, + persistLayoutSnapshot + }), + [ + expandedPaneIdRef, + expandedStyleSnapshotRef, + containerRef, + managerRef, + setExpandedPaneId, + setTabPaneExpanded, + pendingPaneSizeRefreshFrameIdsRef, + tabId, + persistLayoutSnapshot + ] + ) +}