Repository navigation
fix(terminal): stop keyboard effect re-registering on every pane render - #12348
Conversation
…er (STA-3291) 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
|
No actionable comments were generated in the recent review. 🎉 ℹ️ Recent review info⚙️ Run configurationConfiguration used: Repository UI Review profile: CHILL Plan: Pro Plus Run ID: 📒 Files selected for processing (3)
📝 WalkthroughWalkthroughAdded 🚥 Pre-merge checks | ✅ 4 | ❌ 1❌ Failed checks (1 warning)
✅ Passed checks (4 passed)
✨ Finishing Touches 💡 1📝 Generate docstrings 💡
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
Electron QA evidenceValidated pushed HEAD
Initial loaded state: After 50 title-driven renders and a post-storm keyboard round trip: Remaining platform gap: this macOS Electron run does not automate a real Windows OS IME/ConPTY session; simulated Windows keyboard/IME behavior is covered by the focused renderer tests and CI, while the Electron proof here covers the listener-stability invariant and visible terminal input path. |
…er (STA-3291) (stablyai#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 stablyai#12269


Summary
TerminalPanecalledcreateExpandCollapseActions()bare in its render body. The factory mints five fresh closures per call, and four of them (setExpandedPane,restoreExpandedLayout,refreshPaneSizes,toggleExpandPane) are dependencies ofuseTerminalKeyboardShortcuts— so the terminal keyboard effect tore down and re-registered its sevenwindowlisteners on everyTerminalPanerender, and every piece of effect-owned IME state (modifiedEnterChordOwner,heldImeEnterModifiers,nativeOnlyShortcutTracker,deferredNewlineSender,observedEnterKeydownTimeStamps,optionKeyLocation) was silently reset each time. Any render landing mid-chord or mid-composition wiped tracker state — the class of bug behind the STA-3128 symptom that #12265 patched downstream.Fix:
useExpandCollapseActions, a memoized wrapper inexpand-collapse.tskeyed on field-level identities (all stable per mounted pane), so the actions — and therefore the keyboard effect registration — are stable across renders. The raw factory is unchanged for non-hook callers/tests.Live measurement (dev app, CDP-instrumented
windowlistener add/remove counts, single pane running 50 OSC title updates — title updates are what drives pane renders in a busy TUI; raw output and echo drive none):seq 1 20000output burstFixes #12269 (STA-3291)
Screenshots
No visual change.
Testing
pnpm typecheck(all three projects, cleantsbuildinfo)pnpm test— targeted:expand-collapse-render-stability.test.ts(new),expand-collapse.test.ts,keyboard-handlers.test.ts,keyboard-handlers-ime.test.tsx,keyboard-handlers-ime-enter-keyup.test.tsx— 42/42 passpnpm lint— oxlint + react-doctor + oxfmt on changed files via pre-commit;lint-react-doctor-changedgate exits 0pnpm build— not run (renderer-only memoization; no build-surface change)New regression test pins registration stability with TerminalPane-shaped wiring (dep objects rebuilt every render): action identities stable 5/5 across rerenders, remint on
tabIdchange, and exactly onewindowlistener registration across 10 rerenders. Verified non-vacuous: wiring it through the raw factory fails withexpected 11 to be 1.AI Review Report
Reviewed the diff for: dependency-array correctness (hook deps enumerate every field the factory consumes; refs are stable,
setExpandedPaneIdis auseStatesetter,setTabPaneExpandeda store action,persistLayoutSnapshotauseCallbackon[tabId, setTabLayout, worktreeId]— so the memo remints exactly when it should), rules-of-hooks (unconditional call site), and residual churn sources in the keyboard effect's dep array (keybindingsis a direct store selector;terminalShortcutPolicyis a string; remaining entries are refs/useCallbacks — the factory was the only churn source, confirmed by the zero post-fix measurement). Cross-platform: no shortcuts, labels, paths, shell behavior, or Electron platform branches touched; the memoization gap was shared renderer code affecting macOS/Linux/Windows identically (visible symptom historically Windows IME). SSH/remote and folder-workspace paths unaffected — pane wiring is common to all transports.Security Audit
No input handling, command execution, path handling, auth, secrets, dependency, or IPC changes. The change memoizes existing renderer-local closures; listener add/remove surface shrinks. No new attack surface; no follow-up needed.
Notes
keyboard-effect-churn-canary.ts) warns when one mounted keyboard hook re-registers more than 4 times in 2s — catching any future identity-unstable dependency loudly in dev instead of silently re-churning. Keyed per mounted hook instance so tab remounts never false-positive; rate-limited; bounded state; compiled out of production builds (import.meta.env.DEV). A wiring-level test asserts the warn actually fires when a dep churns.TerminalPane.tsx:1642(no-adjust-state-on-prop-change) surfaces when this file is scanned; it is outside this change and left untouched.