Repository navigation
perf(renderer): gate WorktreeCardAgentsBody's send-target subscriptions - #7545
Conversation
|
No actionable comments were generated in the recent review. 🎉 ℹ️ Recent review info⚙️ Run configurationConfiguration used: Repository UI Review profile: CHILL Plan: Pro Run ID: 📥 CommitsReviewing files that changed from the base of the PR and between d191d1d8e962fd55c81d331c34fa03a68f70b70e and d124430. 📒 Files selected for processing (3)
🚧 Files skipped from review as they are similar to previous changes (3)
📝 WalkthroughWalkthroughThis PR adds Changes
Sequence Diagram(s)sequenceDiagram
participant WorktreeCardAgentsBody
participant useAppStore
participant selectSendTargetInputs
participant deriveRunningAgentSendTargets
WorktreeCardAgentsBody->>useAppStore: subscribe with useShallow(selectSendTargetInputs(s, worktreeId))
useAppStore->>selectSendTargetInputs: evaluate against agentSendPopoverTargetMode
selectSendTargetInputs-->>useAppStore: EMPTY_SEND_TARGET_INPUTS or live map refs
useAppStore-->>WorktreeCardAgentsBody: sendTargetInputs
WorktreeCardAgentsBody->>deriveRunningAgentSendTargets: deriveRunningAgentSendTargets(sendTargetInputs, worktreeId)
deriveRunningAgentSendTargets-->>WorktreeCardAgentsBody: sendTargetsByPaneKey
Estimated code review effort: Medium Related PRs: None identified. Suggested labels: refactor, tests Suggested reviewers: None identified. 🚥 Pre-merge checks | ✅ 4 | ❌ 1❌ Failed checks (1 warning)
✅ Passed checks (4 passed)
✨ Finishing Touches📝 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 |
There was a problem hiding this comment.
🧹 Nitpick comments (2)
src/renderer/src/components/sidebar/worktree-card-send-target-inputs.ts (2)
32-38: 📐 Maintainability & Code Quality | 🔵 Trivial | ⚡ Quick winDuplicated worktree-match predicate risks drift.
This gate (
s.agentSendPopoverTargetMode?.worktreeId !== worktreeId) duplicates the exact condition computed independently inWorktreeCardAgents.tsxasisAgentSendTargetModeActive(agentSendPopoverTargetMode?.worktreeId === worktreeId). If the "is this worktree the active send target" rule ever needs an extra condition (e.g. checkingstatus === 'open'), it's easy to update one site and miss the other, causingsendTargetInputsandisAgentSendTargetModeActiveto disagree.Consider extracting a shared predicate (e.g.
isSendTargetPopoverActiveFor(mode, worktreeId)) used by both this selector and the component.♻️ Proposed shared predicate
+export function isSendTargetPopoverActiveFor( + mode: SendTargetInputsState['agentSendPopoverTargetMode'], + worktreeId: string +): boolean { + return mode?.worktreeId === worktreeId +} + export function selectSendTargetInputs( s: SendTargetInputsState, worktreeId: string ): RunningAgentTargetState { - if (s.agentSendPopoverTargetMode?.worktreeId !== worktreeId) { + if (!isSendTargetPopoverActiveFor(s.agentSendPopoverTargetMode, worktreeId)) { return EMPTY_SEND_TARGET_INPUTS }And in
WorktreeCardAgents.tsx:- const isAgentSendTargetModeActive = agentSendPopoverTargetMode?.worktreeId === worktreeId + const isAgentSendTargetModeActive = isSendTargetPopoverActiveFor(agentSendPopoverTargetMode, worktreeId)
17-23: 📐 Maintainability & Code Quality | 🔵 Trivial | ⚡ Quick winConsider
Object.freezeon the shared empty singleton.
EMPTY_SEND_TARGET_INPUTSis returned as the shared reference for every idleWorktreeCardAgentsBodyinstance app-wide. Nothing currently mutates it, but freezing costs nothing and guards against a future accidental write silently corrupting state across every mounted card.🛡️ Proposed fix
-export const EMPTY_SEND_TARGET_INPUTS: RunningAgentTargetState = { +export const EMPTY_SEND_TARGET_INPUTS: RunningAgentTargetState = Object.freeze({ agentStatusByPaneKey: {}, tabsByWorktree: {}, terminalLayoutsByTabId: {}, ptyIdsByTabId: {}, runtimePaneTitlesByTabId: {} -} +})
ℹ️ Review info
⚙️ Run configuration
Configuration used: Repository UI
Review profile: CHILL
Plan: Pro
Run ID: b087a0b4-8000-4caf-a3da-b8d2de4410b0
📥 Commits
Reviewing files that changed from the base of the PR and between 3826169 and d191d1d8e962fd55c81d331c34fa03a68f70b70e.
📒 Files selected for processing (3)
src/renderer/src/components/sidebar/WorktreeCardAgents.tsxsrc/renderer/src/components/sidebar/worktree-card-send-target-inputs.test.tssrc/renderer/src/components/sidebar/worktree-card-send-target-inputs.ts
d191d1d to
5cf7294
Compare
WorktreeCardAgentsBody (mounted once per worktree showing inline agents) opened five whole-map Zustand subscriptions — agentStatusByPaneKey, tabsByWorktree, terminalLayoutsByTabId, ptyIdsByTabId, runtimePaneTitlesByTabId. They're read ONLY inside the sendTargetsByPaneKey memo, which early-returns unless the send-target popover targets this worktree. Two of them (runtimePaneTitlesByTabId, agentStatusByPaneKey) churn on every pane-title and agent-status write app-wide, so every mounted agent body re-rendered on every unrelated terminal's title/status change. Collapse the five into one useShallow selector (selectSendTargetInputs) that returns a shared frozen constant while the popover isn't targeting this card, so idle bodies stay referentially equal and stop reacting to the churn — and subscribe fully the instant the popover is ours. Co-authored-by: Orca <[email protected]>
5cf7294 to
d124430
Compare
…ns (stablyai#7545) Co-authored-by: Orca <[email protected]>
Part of a fresh adversarially-verified perf/memory audit (10 finders → 3-skeptic refute-by-default verify → synthesis). This was the highest-value survivor (severity: high), 3/3 skeptic votes, safe in-pattern fix.
1. Description
WorktreeCardAgentsBodyis mounted once per worktree that's currently showing inline agent rows in the sidebar. It opened five separate whole-map Zustand subscriptions:Those five maps are read only inside the
sendTargetsByPaneKeyuseMemo, which early-returns an empty Map whenever the send-target popover isn't targeting this worktree (!isAgentSendTargetModeActive). But subscriptions fire regardless of whether the value is used: two of these maps —runtimePaneTitlesByTabIdandagentStatusByPaneKey— get their top-level reference replaced on every terminal-title change and every agent-status transition (the PTY OSC-title / status path). So in the normal state (popover closed), every mounted agent body across the whole sidebar re-rendered on every unrelated title/status write anywhere in the app.Fix: collapse the five subscriptions into one
useShallowselector (selectSendTargetInputs, extracted to its own module following the existingworktree-card-status-inputs.tsconvention) that returns a shared frozen constant while the popover isn't targeting this card. Idle bodies then stay referentially equal and react only to their ownagents/ack props; the selector subscribes fully the instant the popover targets this worktree.2. Undeniable evidence + measurable improvement
worktree-card-send-target-inputs.test.tsasserts, viazustand/shallow, that:EMPTY_SEND_TARGET_INPUTSreference even afterruntimePaneTitlesByTabId/agentStatusByPaneKeychurn (→useShallowskips the render);WorktreeCardAgentsrender tests (including thesend-targetsuite) pass unchanged.3. ELI5
Every little agent name-tag in the sidebar was listening for every terminal in the whole app to rename itself or change status, and redrawing itself each time — even though it only needed that information for the brief moment you open the "send to agent" menu. Now each name-tag ignores all that chatter until you actually open that menu on its card.
4. Trade-offs that regress the end experience
None material. The only behavior depending on those maps is the send-target popover, and the selector subscribes fully the instant the popover targets this worktree, so eligibility stays live while the menu is open.
agentStatusEpoch(a cheap scalar) is deliberately left subscribed so stale-boundary freshness flips still re-derive. The one theoretical risk — auseShallowselector dropping a needed field — is covered by the unit test above and by the 41 passing render tests.Made with Orca 🐋