Skip to content

perf(tabs): stop PTY layout writes from re-rendering every tab strip (STA-3328) - #12371

Closed
brennanb2025 wants to merge 7 commits into
mainfrom
brennan/sta-3328-tab-group-render-amplifiers
Closed

brennanb2025 wants to merge 7 commits into
mainfrom
brennan/sta-3328-tab-group-render-amplifiers

Conversation

@brennanb2025

Copy link
Copy Markdown
Contributor

Summary

Second installment of the STA-3328 typing-lag work (first: #12359, the spinner clock). This removes the tab-group render amplifiers confirmed by the terminal-pane React audit — the fan-out path that turns every PTY layout write and store notification into fleet-wide tab-strip reconciliation:

  1. useTabGroupWorkspaceModel subscribed to the global terminalLayoutsByTabId record but only read it inside the activateTerminal callback. Every PTY spawn / attach / reattach in any worktree re-rendered every mounted worktree's TabGroupPanel (hidden ones included — Terminal.tsx renders all mounted split surfaces, hiding via CSS). Now a transient useAppStore.getState() read at call time — the same pattern the callback already used for the runtime-environment lookup — and the record is out of the hook's subscription and the callback's deps.
  2. TabBar's React.memo was permanently defeated: the hook returned commands as a per-render object literal and TabGroupPanel passed 7 inline arrow props. commands is now useMemo'd over stable store actions, and the 7 arrows are hoisted useCallbacks, so the memo can actually bail when nothing tab-related changed.
  3. writePanePtyLayoutBinding rewrote identical pane→PTY bindings on reattach, minting fresh global layout state for a no-op. Now bails when the binding is unchanged.

Measured context (live prod profiling from the STA-3328 investigation): agent status/title/layout writes are continuous under orchestration, and the 200–488ms render bursts they trigger fan out through exactly this path. The remaining burst source (per-status synchronous store commits) is a separate follow-up; this PR makes each burst cheap instead of fleet-wide.

Render-count delta pinned by the new tests: before, one layout write re-rendered every mounted group's model hook (O(writes × mounted worktrees), with a full TabBar reconciliation each due to the defeated memo); after, zero re-renders per layout write, and commands identity survives legitimate re-renders.

Screenshots

No visual change.

Testing

  • pnpm lint — oxlint, react-doctor changed-lines gate (exit 0; one pre-existing warning in an untouched TerminalPane effect surfaces from line drift), oxfmt, max-lines ratchet all clean
  • pnpm typecheck — all three configs pass
  • pnpm test — targeted: all 48 tab-group/tab-bar test files (331 tests) + full terminal-pane suite (2,971 tests) pass; full suite left to CI
  • pnpm build — left to CI
  • Added useTabGroupWorkspaceModel.render-stability.test.tsx with a real zustand store and real React rendering (the existing focus-test harness neuters useMemo and the store, which would make these assertions vacuous). Both tests verified to fail against the pre-fix source (2/2 red on parent): a global layout write must not re-render the hook, and commands must keep identity across store-driven re-renders.

AI Review Report

Reviewed for: stale-read hazards from the transient getState() (none — activateTerminal reads the layout at invocation time, which is fresher than the previously closed-over snapshot; the callback already used getState() for the runtime-environment lookup); useMemo/useCallback dep completeness (all referenced values listed; store actions are stable zustand references; commands identity changes only when a constituent callback genuinely changes, e.g. groupTabs); behavior parity of the hoisted TabGroupPanel handlers (bodies unchanged, same closure values via deps); the binding-guard's skip path (only skips when existingBindings[leafId] === ptyId — the written layout would have been structurally identical; the clear path with its active-leaf repair is untouched). Cross-platform: renderer-only React/zustand change, no shortcuts, labels, paths, or shell behavior; SSH reconnect and folder workspaces are the primary beneficiaries (reattach storms were the worst case). The deliberately-skipped sibling fix: setTabLayout identity bailout needs a structural comparator on TerminalLayoutSnapshot — not a two-liner, deferred to keep this PR mechanical.

Security Audit

No input handling, command execution, path handling, auth, secrets, or IPC changes. Pure render-layer restructuring: one subscription removed, closures memoized, one redundant store write skipped. Test file mocks are scoped to the existing test-mock surface.

Notes

  • Follow-ups tracked on STA-3328: per-agent-status write coalescing (the burst source), setTabLayout structural bailout, split-divider drag off the store (drag-only jank), and the audit's medium-tier findings.
  • The react-doctor warning at TerminalPane.tsx:1647 is a pre-existing effect outside this diff (surfaces via line drift); the changed-lines gate passes.

The shared 12Hz JS clock wrote el.style.transform on every mounted spinner
(41 live = ~490 main-thread style writes/s), keeping style perpetually dirty
and delaying keystroke dispatch (measured typing inputDelay p90 363ms; 19ms
with the writes stopped). Replace it with a steps(12) CSS transform animation
so rotation runs on the compositor; one mount-time animation-delay write
preserves the shared-epoch phase sync.
…(STA-3328)

Three amplifiers from the terminal-pane React audit, all on the constant
orchestration path:
- useTabGroupWorkspaceModel subscribed to the global terminalLayoutsByTabId
  record but only read it inside the activateTerminal callback, so every PTY
  spawn/attach/reattach re-rendered every mounted worktree's tab strip; now a
  transient getState read.
- The hook returned commands as a per-render literal and TabGroupPanel passed
  7 inline arrows, permanently defeating TabBar's React.memo; now memoized.
- writePanePtyLayoutBinding rewrote an identical pane->PTY binding on
  reattach, churning the global record; now bails when unchanged.
@coderabbitai

coderabbitai Bot commented Aug 4, 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: a0cec589-bcd0-410d-b1a2-1cabac5ec80f

📥 Commits

Reviewing files that changed from the base of the PR and between 39c3c58 and 18a7b92.

📒 Files selected for processing (4)
  • src/renderer/src/components/tab-group/TabGroupPanel.tsx
  • src/renderer/src/components/tab-group/useTabGroupWorkspaceModel.render-stability.test.tsx
  • src/renderer/src/components/tab-group/useTabGroupWorkspaceModel.ts
  • src/renderer/src/components/terminal-pane/TerminalPane.tsx
🚧 Files skipped from review as they are similar to previous changes (4)
  • src/renderer/src/components/tab-group/TabGroupPanel.tsx
  • src/renderer/src/components/tab-group/useTabGroupWorkspaceModel.render-stability.test.tsx
  • src/renderer/src/components/tab-group/useTabGroupWorkspaceModel.ts
  • src/renderer/src/components/terminal-pane/TerminalPane.tsx

📝 Walkthrough

Walkthrough

The workspace model no longer subscribes to terminal layout state. It reads the active terminal leaf transiently and memoizes its commands object. The tab group panel memoizes tab-management handlers before passing them to TabBar. The terminal pane skips PTY binding writes when the requested binding already exists. A new render-stability test suite verifies subscription isolation and command reference stability.

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 0.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 summarizes the primary performance change: preventing PTY layout writes from re-rendering tab strips.
Description check ✅ Passed The description follows the repository template and covers the summary, visual impact, testing, AI review, security audit, and follow-up notes.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.
✨ 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.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 1


ℹ️ Review info
⚙️ Run configuration

Configuration used: Repository UI

Review profile: CHILL

Plan: Pro Plus

Run ID: b108ce21-b40b-4af7-9e15-dca259d7e5f8

📥 Commits

Reviewing files that changed from the base of the PR and between 9ec4907 and 9ac7735.

📒 Files selected for processing (11)
  • src/renderer/src/assets/main.css
  • src/renderer/src/components/AgentStateDot.test.ts
  • src/renderer/src/components/AgentWorkingSpinner.test.tsx
  • src/renderer/src/components/AgentWorkingSpinner.tsx
  • src/renderer/src/components/sidebar/StatusIndicator.test.ts
  • src/renderer/src/components/tab-group/TabGroupPanel.tsx
  • src/renderer/src/components/tab-group/useTabGroupWorkspaceModel.render-stability.test.tsx
  • src/renderer/src/components/tab-group/useTabGroupWorkspaceModel.ts
  • src/renderer/src/components/terminal-pane/TerminalPane.tsx
  • src/renderer/src/lib/agent-spinner-clock.test.ts
  • src/renderer/src/lib/agent-spinner-clock.ts
💤 Files with no reviewable changes (2)
  • src/renderer/src/lib/agent-spinner-clock.ts
  • src/renderer/src/lib/agent-spinner-clock.test.ts

@coderabbitai

coderabbitai Bot commented Aug 5, 2026

Copy link
Copy Markdown
Contributor

Note

GitHub couldn't provide a complete incremental comparison for this pull request, so CodeRabbit is performing a full review instead. This review may take a little longer.

@nwparker

Copy link
Copy Markdown
Contributor

Closing for now: item 3 is covered by setTabLayout's terminalLayoutEqual bail. Items 1–2 still reproduce on main: useTabGroupWorkspaceModel subscribes to terminalLayoutsByTabId even though it's only read in activateTerminal, and the TabBar memo is defeated. But the gain is unmeasured, and the rebase is effectively a rewrite. A small PR that reads it via getState() in the callback, with a render profile, would be welcome.

@nwparker nwparker closed this Oct 14, 2026
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.

2 participants