Skip to content

perf(runtime): memoize mobile snapshots for mounted terminal worktrees - #12383

Merged
brennanb2025 merged 1 commit into
mainfrom
brennanb2025/mounted-surface-capture-memo
Aug 4, 2026
Merged

brennanb2025 merged 1 commit into
mainfrom
brennanb2025/mounted-surface-capture-memo

Conversation

@brennanb2025

Copy link
Copy Markdown
Contributor

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 terminalHiddenViewParking off), 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. MountedTerminalSurfaceCapture snapshots, 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 — registeredTabs is 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 oxlint on both changed files (plus pre-commit oxlint + React Doctor + oxfmt)
  • pnpm run typecheck:web
  • pnpm test — the wrapper does not run in this environment (windows-native-registry rebuild); vitest was invoked directly:
  • pnpm exec vitest run --config config/vitest.config.ts src/renderer/src/runtime/ — 58 files / 569 tests pass
  • All out-of-directory importers of sync-runtime-graph (terminal-pane/, hooks/, store/slices/, workspace-activation-terminal-focus) — 370 files / 5317 tests pass
  • pnpm build
  • Added 4 tests in sync-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 direction

New tests

  • reuses a mounted worktree snapshot when its live pane state is unchanged (zero content-build reads + object identity)
  • publishes a live pty rebinding on a mounted worktree (new ptyId reaches the snapshot; unmounted sibling untouched)
  • publishes a live pane split on a mounted worktree (second leaf appears)
  • falls back to the persisted pty binding after the surface unmounts (rebuild fires, saved ptyIdsByLeafId takes 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:

  • Capture completeness: the build path's only live-state reads were registeredTabs.get() in getRuntimeLeafIdsForTerminal and buildMobileTerminalSurfaceTabs (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.
  • Staleness (the P0-shaped risk post-perf(runtime): withhold unchanged mobile snapshots from the graph payload #12245): a false "unchanged" would be withheld from main permanently. The capture is rebuilt from live sources on every publication and compared by value, so any live change is witnessed; verified by the pty-rebinding and pane-split tests.
  • Semantics preservation: the active-leaf fallback chain (hasLiveActivePane distinguishes live-null from no-live-pane), the pty resolution chain (live → saved → single-tab fallback), and the persisted-leaf path when getPanes() is empty all mirror the pre-change expressions one-for-one.
  • Cost: capture is O(panes) per mounted tab (a DOM walk the build already did every publication) and is only allocated for worktrees with registered tabs; the 200+ unmounted tail shares the empty-map singleton and compares by reference.
  • Cross-platform (macOS/Linux/Windows): renderer-only TypeScript; no paths, shortcuts, shell, or Electron platform APIs touched. The 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

  • The capture also removes the mounted-tail deep-compare/backstop churn flagged in the scan; the remaining P2s from that scan (comparator exhaustiveness enforcement, unmemoized agent-status index, two-generation retention in the backstop) are intentionally not bundled here.
  • If serializePaneTree output 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.

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

coderabbitai Bot commented Aug 4, 2026

Copy link
Copy Markdown
Contributor

Review Change Stack

📝 Walkthrough

Walkthrough

The 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)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 7.69% 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 and concisely describes memoizing mobile snapshots for mounted terminal worktrees.
Description check ✅ Passed The description covers all required sections, documents completed and skipped checks, and provides testing, review, security, and platform details.
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.

🧹 Nitpick comments (1)
src/renderer/src/runtime/sync-runtime-graph.ts (1)

96-103: 📐 Maintainability & Code Quality | 🔵 Trivial | 💤 Low value

Reduce 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

📥 Commits

Reviewing files that changed from the base of the PR and between ce8b778 and 454bf3b.

📒 Files selected for processing (2)
  • src/renderer/src/runtime/sync-runtime-graph-publication-cost.test.ts
  • src/renderer/src/runtime/sync-runtime-graph.ts

@brennanb2025

Copy link
Copy Markdown
Contributor Author

QA evidence

Validated the branch build at 454bf3bf using the isolated Electron instance trumpetfish @ brennanb2025/mounted-surface-capture-memo.

  • Electron: activated the PR worktree, mounted Terminal 1, and split it through the real terminal control. The visible app rendered both panes; backing state held two distinct leaf IDs and PTY bindings.
  • iOS Simulator: iPhone 17 Pro (iOS 26.5), Orca Mobile dev client, Metro LAN bundle, paired to this same branch runtime. The worktree session rendered 3 tabs: the two Terminal 1 split leaves plus Mobile QA, and opened live terminal output.
  • Automated verification: focused runtime suites 74/74, full runtime suite 569/569, typecheck:web, changed-file oxlint, and git diff --check all passed.

Electron — mounted split surface

Electron mounted split

iOS — paired mobile session receives both mounted split leaves

iOS paired mobile session

@brennanb2025
brennanb2025 merged commit 64b81cd into main Aug 4, 2026
43 checks passed
enrick-dev pushed a commit to enrick-dev/orca that referenced this pull request Oct 15, 2026
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.
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.

1 participant