Repository navigation
perf(runtime): memoize mobile snapshots for mounted terminal worktrees - #12383
Conversation
The per-worktree rebuild memo from #12207 refused to skip any worktree with a registered TerminalPane, because builders read live PaneManager/DOM state the memo could not witness. Hidden worktrees stay mounted (8 parked + 12 retained by default, unbounded with terminalHiddenViewParking off), so that set rebuilt on every publication — on the always-on hosts the memo targeted, ~20 worktrees paid the full content build plus the new inputs build and deep compare every tick. Capture the live reads instead: snapshot pane leaf ids, the live active leaf, the serialized pane tree, and per-pane numeric/pty bindings once per publication into MountedTerminalSurfaceCapture, compare captures by value in the memo, and have the builders read only the capture. Live state outside the capture is now unrepresentable in a builder, so the skip stays provably complete — and a mounted worktree whose panes did not change reuses its snapshot like any other.
📝 WalkthroughWalkthroughThe runtime graph now captures mounted terminal state once per publication. It records live panes, layouts, active leaves, numeric pane IDs, and PTY IDs. Snapshot reuse compares captured state structurally, so unchanged mounted worktrees reuse prior snapshots. Terminal tab generation uses captured state and retains persisted fallbacks. Tests cover unchanged surfaces, PTY changes, pane additions, and terminal unregistration. 🚥 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 |
There was a problem hiding this comment.
🧹 Nitpick comments (1)
src/renderer/src/runtime/sync-runtime-graph.ts (1)
96-103: 📐 Maintainability & Code Quality | 🔵 Trivial | 💤 Low valueReduce this comment to one concise rationale.
The comment repeats implementation details that the type fields already show. Keep one sentence that states why the capture exists.
Proposed change
-/** - * Live PaneManager/DOM reads for one mounted terminal tab, captured once per - * publication. - * - * Why: builders read this instead of the registry, so live state the memo - * cannot witness is unrepresentable — mounted worktrees memoize like the rest - * instead of rebuilding on every publication. - */ +// Capture mounted terminal state once so unchanged worktrees can reuse snapshots.As per coding guidelines, comments must be concise, limited to non-obvious information, and preferably one line.
Source: Coding guidelines
ℹ️ Review info
⚙️ Run configuration
Configuration used: Repository UI
Review profile: CHILL
Plan: Pro Plus
Run ID: 993c1e0c-d33b-4fe3-af15-b10fb1e4fbf2
📒 Files selected for processing (2)
src/renderer/src/runtime/sync-runtime-graph-publication-cost.test.tssrc/renderer/src/runtime/sync-runtime-graph.ts
QA evidenceValidated the branch build at
Electron — mounted split surfaceiOS — paired mobile session receives both mounted split leaves |
stablyai#12383) The per-worktree rebuild memo from stablyai#12207 refused to skip any worktree with a registered TerminalPane, because builders read live PaneManager/DOM state the memo could not witness. Hidden worktrees stay mounted (8 parked + 12 retained by default, unbounded with terminalHiddenViewParking off), so that set rebuilt on every publication — on the always-on hosts the memo targeted, ~20 worktrees paid the full content build plus the new inputs build and deep compare every tick. Capture the live reads instead: snapshot pane leaf ids, the live active leaf, the serialized pane tree, and per-pane numeric/pty bindings once per publication into MountedTerminalSurfaceCapture, compare captures by value in the memo, and have the builders read only the capture. Live state outside the capture is now unrepresentable in a builder, so the skip stays provably complete — and a mounted worktree whose panes did not change reuses its snapshot like any other.


Summary
Follow-up to #12207, found during a release scan of #12207 + #12245: the per-worktree mobile-snapshot rebuild memo refused to skip any worktree with a mounted TerminalPane, because the builders read live PaneManager/DOM state the memo could not witness. Hidden worktrees stay mounted (8 hot-parked + 12 retained by default; unbounded with
terminalHiddenViewParkingoff), so on the always-on hosts the memo targeted, ~20 worktrees still paid a full content build on every publication — plus the new inputs build and deep compare #12207 added.This PR captures the live reads instead of bypassing the memo.
MountedTerminalSurfaceCapturesnapshots, once per publication per mounted tab: the live pane leaf ids, the live active leaf, the serialized pane DOM tree, and each pane's numeric-id/pty bindings. The builders now read only the capture —registeredTabsis no longer reachable from the build path — so live state outside the capture is unrepresentable, preserving the compiler-proof input-set property #12207 established. The memo compares captures by value, so a mounted worktree whose panes did not change reuses its snapshot like any other, and any live change (split, pty rebinding, active-pane move, unmount) still forces a rebuild.No behavior change to snapshot content: the capture holds exactly the values the builders previously read live, resolved at the same point in the same synchronous build pass.
Screenshots
No visual change.
Testing
pnpm exec oxlinton both changed files (plus pre-commit oxlint + React Doctor + oxfmt)pnpm run typecheck:webpnpm test— the wrapper does not run in this environment (windows-native-registryrebuild); vitest was invoked directly:pnpm exec vitest run --config config/vitest.config.ts src/renderer/src/runtime/— 58 files / 569 tests passsync-runtime-graph(terminal-pane/,hooks/,store/slices/,workspace-activation-terminal-focus) — 370 files / 5317 tests passpnpm buildsync-runtime-graph-publication-cost.test.ts; the skip test (reuses a mounted worktree snapshot when its live pane state is unchanged) fails against the pre-fix source (verified via stash), and the three live-change tests pin the safety directionNew tests
ptyIdsByLeafIdtakes over)These also close a review gap:
hasMountedTerminalSurface— the guard this PR replaces — previously had no test that would fail if it were dropped.AI Review Report
Reviewed as part of a 4-reviewer release scan of #12207 + #12245 (skip-safety, contract, perf, security), which produced this finding: the mounted-set assumption ("mounted ≈ active worktree") was wrong, making #12207 a small net regression for the mounted tail. Risks checked for this fix:
registeredTabs.get()ingetRuntimeLeafIdsForTerminalandbuildMobileTerminalSurfaceTabs(getPanes,getActivePane/getLeafId,getContainer→serializePaneTree,getNumericIdForLeaf,getPtyIdForPane). All six reads are in the capture; after this change neither builder references the registry, so a future live read must be added to the capture type to compile.hasLiveActivePanedistinguishes live-null from no-live-pane), the pty resolution chain (live → saved → single-tab fallback), and the persisted-leaf path whengetPanes()is empty all mirror the pre-change expressions one-for-one.typeof HTMLElement !== 'undefined'guard moved intact, so node-side test environments are unaffected. SSH/remote and folder-workspace paths flow through the same store slices and are unaffected.Security Audit
No new IPC surface, command execution, path handling, or input parsing. The change is renderer-local memoization: it reads existing in-process PaneManager/DOM state and compares it by value. The IPC payload shape (
RuntimeSyncWindowGraph) is untouched; main-process code is untouched. No dependencies added. Captured pty ids were already published in snapshots pre-change; the capture holds them at most one publication generation.Notes
serializePaneTreeoutput ever jitters (e.g. float pane sizes on window resize), the capture compares unequal and rebuilds — identical behavior to before this PR, never a suppressed publication.