Skip to content

fix(terminal): stop keyboard effect re-registering on every pane render - #12348

Merged
brennanb2025 merged 1 commit into
mainfrom
brennanb2025/issue-3291-user-report
Aug 3, 2026
Merged

brennanb2025 merged 1 commit into
mainfrom
brennanb2025/issue-3291-user-report

Conversation

@brennanb2025

@brennanb2025 brennanb2025 commented Aug 3, 2026 •

Copy link
Copy Markdown
Contributor

Summary

TerminalPane called createExpandCollapseActions() bare in its render body. The factory mints five fresh closures per call, and four of them (setExpandedPane, restoreExpandedLayout, refreshPaneSizes, toggleExpandPane) are dependencies of useTerminalKeyboardShortcuts — so the terminal keyboard effect tore down and re-registered its seven window listeners on every TerminalPane render, 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 in expand-collapse.ts keyed 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 window listener 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):

Scenario Before After
Idle 5s 0 listener ops 0
Typing 31 chars 0 0
seq 1 20000 output burst 0 0
50 OSC title updates 700 listener ops (7 listeners × 50 re-registrations, 1:1 with renders) 0 (all 50 title updates confirmed processed)

Fixes #12269 (STA-3291)

Screenshots

No visual change.

Testing

  • pnpm typecheck (all three projects, clean tsbuildinfo)
  • 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 pass
  • pnpm lint — oxlint + react-doctor + oxfmt on changed files via pre-commit; lint-react-doctor-changed gate exits 0
  • pnpm build — not run (renderer-only memoization; no build-surface change)
  • Added or updated high-quality tests that would catch regressions, or explained why tests were not needed

New regression test pins registration stability with TerminalPane-shaped wiring (dep objects rebuilt every render): action identities stable 5/5 across rerenders, remint on tabId change, and exactly one window listener registration across 10 rerenders. Verified non-vacuous: wiring it through the raw factory fails with expected 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, setExpandedPaneId is a useState setter, setTabPaneExpanded a store action, persistLayoutSnapshot a useCallback on [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 (keybindings is a direct store selector; terminalShortcutPolicy is 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

  • Follow-up hardening in test(terminal): pin IME chord survival across mid-composition re-renders #12357 (this PR merged while the canary commit was in flight, so it landed separately): a dev-only churn canary (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.
  • Latent since fix(terminal): stop Windows IME keys from triggering shortcuts #12265 removed the visible IME symptom; this corrects the underlying invariant ("one claim until release" now actually bounds a chord's lifetime) and stops 14 listener ops per render per mounted pane.
  • A pre-existing react-doctor warning at TerminalPane.tsx:1642 (no-adjust-state-on-prop-change) surfaces when this file is scanned; it is outside this change and left untouched.

…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
@coderabbitai

coderabbitai Bot commented Aug 3, 2026

Copy link
Copy Markdown
Contributor

Review Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Repository UI

Review profile: CHILL

Plan: Pro Plus

Run ID: 07498ec8-20d8-41cd-ab17-b11adcc6ca54

📥 Commits

Reviewing files that changed from the base of the PR and between a4944f5 and 75e5648.

📒 Files selected for processing (3)
  • src/renderer/src/components/terminal-pane/TerminalPane.tsx
  • src/renderer/src/components/terminal-pane/expand-collapse-render-stability.test.ts
  • src/renderer/src/components/terminal-pane/expand-collapse.ts

📝 Walkthrough

Walkthrough

Added useExpandCollapseActions with useMemo to preserve action identities while its state dependencies remain unchanged. Updated TerminalPane to use the hook. Added tests for action identity changes when tabId changes and for stable beforeinput listener registration across repeated rerenders.

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 50.00% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Title check ✅ Passed The title clearly identifies the fix for repeated keyboard effect registration during terminal pane renders.
Description check ✅ Passed The description covers the required sections, implementation, testing, review, security, platform scope, and the unrun build with justification.
Linked Issues check ✅ Passed The changes satisfy #12269 by memoizing action identities, preventing listener churn, preserving tab changes, and adding regression tests.
Out of Scope Changes check ✅ Passed The changes are limited to terminal action memoization, keyboard listener stability, and related regression coverage.
✨ Finishing Touches 💡 1
📝 Generate docstrings 💡
  • Create stacked PR
  • Commit on current branch

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.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@brennanb2025

Copy link
Copy Markdown
Contributor Author

Electron QA evidence

Validated pushed HEAD 75e5648053deae829909e4329f9bb7cf918cd1a7 in the intended dev instance (tubesnout @ brennanb2025/issue-3291-user-report).

  • Loaded the real local terminal for this worktree through Electron/CDP.
  • Typed 50 distinct OSC title updates through the terminal input surface; the visible tab reached listener-stability-50.
  • Instrumented the keyboard effect's listener types during that render storm: 0 adds, 0 removes.
  • Typed again after the storm and observed KEYBOARD_INPUT_OK in the terminal.
  • Renderer console: 0 errors.

Initial loaded state:

orca-pr12348-terminal-loaded.png

After 50 title-driven renders and a post-storm keyboard round trip:

orca-pr12348-keyboard-after-render-storm.png

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.

@brennanb2025
brennanb2025 merged commit 7de40ff into main Aug 3, 2026
43 checks passed
enrick-dev pushed a commit to enrick-dev/orca that referenced this pull request Oct 15, 2026
…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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug]: terminal keyboard effect re-registers on every TerminalPane render (unmemoized createExpandCollapseActions)

1 participant