Skip to content

fix(browser): keep browser tabs rendering across worktree switches (STA-3228) - #12137

Merged
brennanb2025 merged 3 commits into
mainfrom
brennan/sta-3228-old-browser-tab-cant-be-rendered-maybe-html-only
Aug 2, 2026
Merged

brennanb2025 merged 3 commits into
mainfrom
brennan/sta-3228-old-browser-tab-cant-be-rendered-maybe-html-only

Conversation

@brennanb2025

Copy link
Copy Markdown
Contributor

Summary

Fixes STA-3228: a browser tab that was open in a worktree went permanently blank after switching to another worktree and back — the URL bar still showed the address, but the page area rendered nothing, and reload silently did nothing.

Two layered defects:

  1. Trigger — WorktreeSplitSurface unmounted BrowserPaneOverlayLayer for hidden worktrees whenever a targeted background mount restriction existed (agent wake/resume sets one constantly). Unmounting the overlay pulls the persistent <webview> slot divs out of the DOM, and Electron destroys the guest the moment a <webview> leaves the DOM — defeating the design from fix(browser): keep webview guests alive across worktree switches #6408 that renders browser panes at worktree level precisely so guests survive switches.
  2. Permanence — ensureBrowserPageViewport returned the cached viewport shell even after its slot root had been unmounted, so when the worktree came back, the webview subtree was never re-appended to the live DOM. The tab stayed blank forever and the reload button threw The WebView must be attached to the DOM and the dom-ready event emitted before this method can be called (observed in the renderer console during repro).

Fix:

  • Keep BrowserPaneOverlayLayer mounted for hidden worktrees. Slots already park their pane subtrees when the worktree is hidden (shouldMountPane), so a background-woken worktree only pays for empty slot divs. EmulatorPaneOverlayLayer keeps the old gating — it doesn't use the persistent-slot pattern.
  • Make ensureBrowserPageViewport detect a replaced slot root and drop the stranded shell so the caller rebuilds against the live root. This also self-heals any remaining path that unmounts slots (cross-worktree tab moves, surface eviction): the existing parent-drift check in ensureBrowserPageWebview then rebuilds the webview, turning "blank forever" into "reloads once".

Screenshots

Repro on main (tab created, worktree switched away and back — blank, reload dead):

before: blank browser pane after worktree switch

Same sequence with the fix (guest survives without reloading — same in-page JS timestamp, no new HTTP request; reload button works):

after: page still rendered after worktree switch

Testing

  • pnpm lint (oxlint + react-doctor changed-lines on touched files)
  • pnpm typecheck
  • pnpm test (browser-pane suite 32 files/191 tests; Terminal substring sweep 462 files/5156 tests)
  • pnpm build
  • Added or updated high-quality tests that would catch regressions, or explained why tests were not needed

New tests in browser-page-viewport.test.ts: cached-viewport reuse under a stable root, parked viewport preserved while the root is unregistered, and rebuild under a replacement root (asserts the stale shell is detached and the rebuilt shell is connected to the new root).

Live Electron QA (dev build, CDP): reproduced the blank pane + dead reload on unfixed code, then verified with the fix that (a) slot/shell/webview stay in the DOM while the worktree is hidden, (b) the page renders after switching back with the guest's state intact (unchanged in-page timestamp, no new server request), (c) reload issues a real request, and (d) a session-restored tab renders after app restart.

AI Review Report

Reviewed the mount-gating change for perf regressions (background wakes previously skipped the overlay): slots park pane subtrees for hidden worktrees, so no webview guests or BrowserPane chrome mount from this change alone. Reviewed the viewport self-heal for interactions with destroyPersistentWebview (which removes the current viewport before resolveContainer rebuilds it) and the floating-panel slot registration (FloatingBrowserSlot mints new tab ids, so no cross-host root swap for a live id). Checked the no-root case keeps returning the parked viewport so hidden-state callers see unchanged behavior. Cross-platform: renderer-only DOM/React change, no shortcuts, labels, paths, or shell behavior touched; nothing platform-conditional on macOS/Linux/Windows is affected.

Security Audit

No input handling, command execution, path handling, auth, secrets, or dependency changes. No new IPC: the change only alters when existing renderer components mount and how a renderer-side DOM cache invalidates. Webview partitions are untouched (partition mismatch still forces a rebuild via the pre-existing check in ensureBrowserPageWebview).

Notes

  • The surface-eviction path (a hidden worktree fully unmounting after long parking) still destroys guests by design; with the self-heal the tab now recovers with one reload instead of dying. If we want guests to survive that too, that's a follow-up.
  • EmulatorPaneOverlayLayer may have an analogous issue on the same conditional; left untouched to keep this PR scoped (worth a separate look).

…TA-3228)

Switching away from a worktree that had a targeted background mount
unmounted BrowserPaneOverlayLayer, pulling the persistent <webview>
slots out of the DOM and killing their guests; the stale viewport cache
then kept rendering into the removed subtree, so the tab stayed blank
forever and reload threw. Keep the overlay mounted for hidden worktrees
(slots park their panes, so this stays cheap) and rebuild cached
viewports whose slot root remounted.
@greptile-apps

greptile-apps Bot commented Aug 2, 2026 •

Copy link
Copy Markdown

Greptile Summary

Fixes two layered defects that caused browser pane tabs to go permanently blank after switching worktrees and back: (1) WorktreeSplitSurface unmounted BrowserPaneOverlayLayer for hidden worktrees whenever a targeted background mount restriction existed (agent wake/resume always sets one), pulling slot <div>s out of the DOM and triggering Electron guest destruction; (2) ensureBrowserPageViewport returned the cached viewport shell even after its slot root was replaced, stranding the shell and making reload permanently dead.

  • Introduces RetainedBrowserPaneOverlayLayer — a thin wrapper that latches hasCommittedMount via useLayoutEffect after the first eligible render commits, keeping slot DOM alive for hidden worktrees without paying for webview guests or BrowserPane chrome.
  • ensureBrowserPageViewport now compares existing.shell.parentElement against the current root and detaches the stranded shell when a new root is present.
  • destroyPersistentWebview gains a preserveViewport flag; ensureBrowserPageWebview passes it on parent-drift teardown so the freshly-rebuilt viewport shell is not simultaneously destroyed.

Confidence Score: 5/5

Safe to merge — renderer-only change, no IPC, no partitions, no auth paths, no platform-conditional code touched.

Both fixes are well-scoped and each edge case (parked state, stable root, replacement root, suspended-before-commit latch) has a dedicated unit test. The preserveViewport flag is narrowly applied only on the parent-drift path where a new viewport was already placed before teardown fires.

Files Needing Attention: No files require special attention; all changed files have matching test coverage and clear inline rationale comments.

Important Files Changed

Filename Overview
src/renderer/src/components/browser-pane/BrowserPaneOverlayLayer.tsx Adds RetainedBrowserPaneOverlayLayer — a memo wrapper that latches hasCommittedMount via useLayoutEffect after the first eligible DOM commit, keeping BrowserPaneOverlayLayer mounted for hidden worktrees.
src/renderer/src/components/browser-pane/browser-page-viewport.ts ensureBrowserPageViewport now detects when existing.shell.parentElement differs from the registered root, removes the stranded shell, and rebuilds under the new root.
src/renderer/src/components/browser-pane/browser-page-webview.ts Passes preserveViewport:true to destroyPersistentWebview on parent-drift teardown, preventing the freshly-rebuilt viewport shell from being removed while the stale webview is cleaned up.
src/renderer/src/components/browser-pane/webview-registry.ts Adds optional preserveViewport flag to destroyPersistentWebview, skipping removeBrowserPageViewport on both the no-webview and live-webview paths when set.
src/renderer/src/components/Terminal.tsx Replaces conditional BrowserPaneOverlayLayer mount with RetainedBrowserPaneOverlayLayer while leaving EmulatorPaneOverlayLayer on the old gating.
src/renderer/src/components/browser-pane/browser-page-viewport.test.ts Adds three new regression tests covering cached-viewport reuse, parked viewport preservation, and rebuild under a replacement root.
src/renderer/src/components/browser-pane/BrowserPaneOverlayLayer.test.tsx Adds tests for RetainedBrowserPaneOverlayLayer: deferred-then-retained lifecycle and suspended-before-commit does not latch hasCommittedMount.
src/renderer/src/components/browser-pane/BrowserPane.webview-preferences.test.ts Adds test verifying destroyPersistentWebview is called with preserveViewport:true on parent-drift and the replacement container is connected with the new webview.

Reviews (3): Last reviewed commit: "fix(browser): latch overlay retention af..." | Re-trigger Greptile

@coderabbitai

coderabbitai Bot commented Aug 2, 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: e08fb236-351e-4b2f-b916-562605d955a7

📥 Commits

Reviewing files that changed from the base of the PR and between a58f3cd and b2dc29c.

📒 Files selected for processing (2)
  • src/renderer/src/components/browser-pane/BrowserPaneOverlayLayer.test.tsx
  • src/renderer/src/components/browser-pane/BrowserPaneOverlayLayer.tsx
🚧 Files skipped from review as they are similar to previous changes (1)
  • src/renderer/src/components/browser-pane/BrowserPaneOverlayLayer.tsx

📝 Walkthrough

Walkthrough

The retained browser overlay mounts after a worktree becomes eligible and remains mounted when eligibility later becomes false. Emulator overlay mounting remains conditional. Cached browser page viewports are reused for the current root, preserved when no root exists, and rebuilt under replacement roots. Parent-drifted webviews preserve their viewports during teardown and mount in resolved replacement containers. Tests cover overlay retention, viewport cache behavior, and webview rebuilding.

🚥 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 identifies the browser rendering fix and references the related issue.
Description check ✅ Passed The description covers the required sections, explains the fix, documents testing and QA, and identifies that the build was not run.
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/components/browser-pane/browser-page-viewport.test.ts (1)

67-83: 📐 Maintainability & Code Quality | 🔵 Trivial | ⚡ Quick win

Exercise the stale-shell removal branch directly.

oldRoot.remove() runs before ensureBrowserPageViewport, so stale.shell.isConnected is already false when the changed code runs. The test verifies replacement allocation, but it does not prove that existing.shell.remove() removes a shell still attached to the old root. Keep the old root connected until after the call, or add a second case for that condition.

Suggested test adjustment
-    oldRoot.remove()
-    registerBrowserOverlaySlotViewport('workspace-1', null)
     const newRoot = mountSlotViewport('workspace-1')
+    expect(oldRoot.contains(stale.shell)).toBe(true)

     const rebuilt = ensureBrowserPageViewport('page-1', 'workspace-1')

+    expect(oldRoot.contains(stale.shell)).toBe(false)
     expect(rebuilt).not.toBeNull()

ℹ️ Review info
⚙️ Run configuration

Configuration used: Repository UI

Review profile: CHILL

Plan: Pro Plus

Run ID: 32a65abb-21ad-4646-ba97-b930d396c0a7

📥 Commits

Reviewing files that changed from the base of the PR and between 99b94a3 and 0909e66.

📒 Files selected for processing (3)
  • src/renderer/src/components/Terminal.tsx
  • src/renderer/src/components/browser-pane/browser-page-viewport.test.ts
  • src/renderer/src/components/browser-pane/browser-page-viewport.ts

@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: 2


ℹ️ Review info
⚙️ Run configuration

Configuration used: Repository UI

Review profile: CHILL

Plan: Pro Plus

Run ID: cce226c9-3872-488b-95a7-0dc22d27e4af

📥 Commits

Reviewing files that changed from the base of the PR and between 0909e66 and a58f3cd.

📒 Files selected for processing (7)
  • src/renderer/src/components/Terminal.tsx
  • src/renderer/src/components/browser-pane/BrowserPane.webview-preferences.test.ts
  • src/renderer/src/components/browser-pane/BrowserPaneOverlayLayer.test.tsx
  • src/renderer/src/components/browser-pane/BrowserPaneOverlayLayer.tsx
  • src/renderer/src/components/browser-pane/browser-page-viewport.ts
  • src/renderer/src/components/browser-pane/browser-page-webview.ts
  • src/renderer/src/components/browser-pane/webview-registry.ts
🚧 Files skipped from review as they are similar to previous changes (1)
  • src/renderer/src/components/browser-pane/browser-page-viewport.ts

Comment on lines +121 to +136
const ensuredWebview = ensureBrowserPageWebview({
browserTabId: 'browser-page-1',
container: replacementContainer,
inputLocked: false,
webviewPartition: 'persist:orca-browser',
resolveContainer
})

expect(registryMocks.destroyPersistentWebview).toHaveBeenCalledWith('browser-page-1', {
preserveViewport: true
})
expect(ensuredWebview?.container).toBe(replacementContainer)
expect(replacementContainer.isConnected).toBe(true)
expect(replacementContainer.lastElementChild).toBe(
ensuredWebview?.webview as unknown as Element
)

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.

📐 Maintainability & Code Quality | 🟡 Minor | ⚡ Quick win

Use a distinct resolved container in this test.

The test passes replacementContainer as both container and the resolveContainer() result. An implementation that ignores resolveContainer() would still pass. Pass a separate requested container and assert that the new webview mounts in replacementContainer.

Proposed test update
+    const requestedContainer = createContainer('requested')
+
     const ensuredWebview = ensureBrowserPageWebview({
       browserTabId: 'browser-page-1',
-      container: replacementContainer,
+      container: requestedContainer,
       inputLocked: false,
       webviewPartition: 'persist:orca-browser',
       resolveContainer
     })
 
+    expect(resolveContainer).toHaveBeenCalledTimes(1)
     expect(ensuredWebview?.container).toBe(replacementContainer)
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
const ensuredWebview = ensureBrowserPageWebview({
browserTabId: 'browser-page-1',
container: replacementContainer,
inputLocked: false,
webviewPartition: 'persist:orca-browser',
resolveContainer
})
expect(registryMocks.destroyPersistentWebview).toHaveBeenCalledWith('browser-page-1', {
preserveViewport: true
})
expect(ensuredWebview?.container).toBe(replacementContainer)
expect(replacementContainer.isConnected).toBe(true)
expect(replacementContainer.lastElementChild).toBe(
ensuredWebview?.webview as unknown as Element
)
const requestedContainer = createContainer('requested')
const ensuredWebview = ensureBrowserPageWebview({
browserTabId: 'browser-page-1',
container: requestedContainer,
inputLocked: false,
webviewPartition: 'persist:orca-browser',
resolveContainer
})
expect(resolveContainer).toHaveBeenCalledTimes(1)
expect(registryMocks.destroyPersistentWebview).toHaveBeenCalledWith('browser-page-1', {
preserveViewport: true
})
expect(ensuredWebview?.container).toBe(replacementContainer)
expect(replacementContainer.isConnected).toBe(true)
expect(replacementContainer.lastElementChild).toBe(
ensuredWebview?.webview as unknown as Element
)

@brennanb2025

brennanb2025 commented Aug 2, 2026 •

Copy link
Copy Markdown
Contributor Author

Electron QA — STA-3228 (b2dc29c)

Validated the final pushed revision in an isolated Orca dev profile attached to the intended worktree through CDP.

  • Opened the deterministic page in Orca, switched from this worktree to main through the rendered worktree search UI, then returned through the same real UI.
  • Before switching: server document load Linux support #1 and guest performance.timeOrigin 1785708162108.6.
  • While hidden: the viewport shell was parked with display:none, while both the shell and webview remained DOM-connected.
  • After returning: the pane rendered normally with the same load Linux support #1 and the same time origin; the server received no second document request.
  • Clicked Orca’s real reload control after returning: the server received document load Fix Ctrl+Backspace not deleting word in terminal #2 and the page visibly rerendered. Renderer console: 0 errors.

Rendered after switching away and back

STA-3228 browser pane rendered after worktree return

Reload still works after the return

STA-3228 browser pane after working reload

Mobile emulator QA is not applicable: this PR only changes desktop renderer/browser-webview paths.

@brennanb2025
brennanb2025 merged commit 0c28936 into main Aug 2, 2026
43 checks passed
brennanb2025 added a commit that referenced this pull request Aug 3, 2026
…12191)

The parent-drift repair path (destroyPersistentWebview with
preserveViewport: true, introduced in #12137) tears down and rebuilds the
webview under the same browserTabId, but unconditionally forgot the tab's
explicit user zoom. BrowserPane then re-seeds from the Settings default on
the next mount, silently resetting per-tab zoom.

Only forget explicit zoom on a real close; a preserveViewport rebuild keeps
the same logical tab, so its zoom must survive.
enrick-dev pushed a commit to enrick-dev/orca that referenced this pull request Oct 15, 2026
…TA-3228) (stablyai#12137)

* fix(browser): keep browser tabs rendering across worktree switches (STA-3228)

Switching away from a worktree that had a targeted background mount
unmounted BrowserPaneOverlayLayer, pulling the persistent <webview>
slots out of the DOM and killing their guests; the stale viewport cache
then kept rendering into the removed subtree, so the tab stayed blank
forever and reload threw. Keep the overlay mounted for hidden worktrees
(slots park their panes, so this stays cheap) and rebuild cached
viewports whose slot root remounted.

* fix(browser): retain live overlay slots without background churn

* fix(browser): latch overlay retention after commit
enrick-dev pushed a commit to enrick-dev/orca that referenced this pull request Oct 15, 2026
…tablyai#12191)

The parent-drift repair path (destroyPersistentWebview with
preserveViewport: true, introduced in stablyai#12137) tears down and rebuilds the
webview under the same browserTabId, but unconditionally forgot the tab's
explicit user zoom. BrowserPane then re-seeds from the Settings default on
the next mount, silently resetting per-tab zoom.

Only forget explicit zoom on a real close; a preserveViewport rebuild keeps
the same logical tab, so its zoom must survive.
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