Skip to content

fix(browser): stop agent tabs in background worktrees reloading every ~10 s - #36245

Merged
AmoabaKelvin merged 1 commit into
mainfrom
browser-bg-tab-reload-budget
Oct 30, 2026
Merged

AmoabaKelvin merged 1 commit into
mainfrom
browser-bg-tab-reload-budget

Conversation

@AmoabaKelvin

@AmoabaKelvin AmoabaKelvin commented Oct 30, 2026 •

Copy link
Copy Markdown
Collaborator
Files Added Deleted Net
Test 2 $\color{#1a7f37}{\Huge{\mathbf{+}}}$​86 $\color{#cf222e}{\Huge{\mathbf{−}}}$​3 $\color{#1a7f37}{\Huge{\mathbf{+}}}$​83
Prod 3 $\color{#1a7f37}{\Huge{\mathbf{+}}}$​37 $\color{#cf222e}{\Huge{\mathbf{−}}}$​13 $\color{#1a7f37}{\Huge{\mathbf{+}}}$​24

ELI5

If an agent was using a browser tab in one workspace while you looked at a different one, the agent's page kept reloading about every 10 seconds. Anything it had typed, and any page state like sessionStorage, was wiped, so agents had to keep starting over. The page now stays put while the agent works.

What Changed

Before: with another workspace on screen, an agent's tab in a hidden workspace loaded 5 times in 45 s. Every load came with a new CDP target, an empty sessionStorage, cleared form fields and stale refs.

After: the same tab loads once and stays the same page. That held for 45 s of one command per second, and again for 2.5 minutes of commands sent 15 s apart with idle gaps longer than the lease.

Mechanism: Orca keeps live browser guests for the 4 hidden workspaces you used most recently and destroys the rest (useTerminalBrowserRetention). "Recent" only meant "you clicked into it", so a workspace an agent drives in the background always sorted last. Its only protection was the 10 s automation lease each command takes. When that lease lapsed, the change itself re-ran the retention pass, which destroyed the webview, and the agent's next command loaded a fresh page.

  • Each retention pass now treats a workspace whose pages are under a paint-retention claim (agent lease, phone driver, paired viewer) as recently used. Once the claim lapses, that workspace sits at the front of the order instead of the back.
  • selectBrowserGuestEvictionWorktreeIds no longer counts a vetoed workspace against the 4 slots. Workspaces that agents or phones are actively using don't push your own recent workspaces out while their claim is held. On main, vetoed workspaces already sat over budget the same way.

Why

The wrong fact was the recency order, so the change is in the one place that keeps it. It covers every route that holds a guest for something other than the user: agents, phones and paired viewers, git worktrees and folder workspaces alike. Alternatives I ruled out:

Known limit, by design: the budget still caps idle guests. If more than 4 hidden workspaces with live tabs are idle (no claim) at once, the least recently used one is still evicted. That one could be an agent page whose agent went quiet for more than 10 s.

Linked Issue

Fixes #36235

Visual Proof

N/A: nothing paints while this happens, because the tab is in a hidden workspace. The proof is the page's own state, read over orca eval in a hidden dev build. One eval per second for 45 s prints performance.timeOrigin|sessionStorage marker:

Before (main):

   7 "1793333721475.4|1"
   8 "1793333732148.7|null"
   8 "1793333743096|null"
   8 "1793333753731.1|null"
   2 "1793333764369.2|null"

After (this branch), followed by 15 s-spaced evals after a 30 s idle gap, with a form value typed into the page:

  35 "1793335403914.8|1"
"result": "1793335403914.8|1|keep@me.dev"   (×5, ~2.5 min)

Testing

  • I manually tested these changes locally
  • Automated tests added/updated, or explained why not below

macOS, in a hidden dev build (ORCA_BACKGROUND_LAUNCH=1) driven over CDP and the dev CLI:

  • Setup: one scratch repo with 6 worktrees. 4 of them have browser tabs and were activated in turn, so they fill the hidden budget. Then a different worktree is activated, and the agent's tab sits in a sixth worktree.
  • main: 5 loads in 45 s. With browserGuestWorktreeRetentionBudget turned off, the same probe shows 1 load, which confirmed the budget as the cause.
  • This branch: 1 load across the 45 s probe and the idle/sparse run. The least recently used user worktree was evicted instead, and live hidden guests stayed at 4.
  • fix(browser): deliver agent clicks and keys to pages that loaded while their pane was hidden #34291 behaviour still holds: on a page that loaded while its pane was hidden, click toggled a checkbox and type filled an input.

Automated:

  • New use-terminal-browser-retention.test.tsx: an agent leases a page in a hidden worktree outside the budget, and the lease lapses. It fails on main (wt-agent evicted) and passes here.
  • browser-guest-worktree-retention.test.ts: one case updated to the new rule that a vetoed worktree doesn't take a slot.
  • Focused suites pass, and the changed-file code-quality and React Doctor gates pass.

AI Disclosure

Review

Two review-loop rounds, each with Codex and a Claude reviewer.

  • Accepted (P2): with the recency change alone, claimed workspaces took budget slots and evicted the user's recent ones. Fixed by keeping vetoed workspaces out of the slot count.
  • Accepted (P3): the veto now builds on the shared paint-retention check instead of repeating it.
  • Not changed (P2, by design): more than 4 idle claimed workspaces can still evict an agent page. Noted above as the budget's limit.
  • Not changed (P3): several workspaces claimed at once get ordered by surface order among themselves. That only matters with more than 4 of them.
  • Round 2: Codex returned NO FINDINGS.

Agent skill upstream boundary

  • Not applicable, or this change copies or mechanically translates no upstream skill-installer source, tests, fixtures, registry entries, path tables, comments, or documentation.

Notes

Ensure no issues in: Security, Cross-platform support (Linux, Windows, Mac), Remote SSH, Mobile, general backwards compatibility, performance

  • Renderer-only, with no platform-specific code, IPC or wire change, so mixed client and host versions are unaffected.
  • Folder workspaces are covered, because the order is built from workspaceSurfaceIds.
  • Phone-driven and paired-viewer pages get the same protection.
  • Performance: each retention pass does one more lookup per workspace. Passes run only when a claim, activation or surface changes.
  • SSH-routed workspaces have a separate, pre-existing reload path that this PR does not touch: SshRoutedBrowserPageGate destroys guests on every remount while its route is re-prepared. It's tracked as a follow-up.

Checklist

  • This PR is small and focused
  • I explained what changed and why (ELI5, the user-facing before/after, the mechanism, and why over the alternatives)
  • Before/after screenshots or videos attached for UI changes, or N/A with reason
  • Self-reviewed for correctness, security, and performance
  • Cross-platform, SSH/remote, and path/shortcut impact considered (or N/A)
  • pnpm lint, pnpm typecheck, pnpm test, and pnpm build pass (or CI will cover; local preferred)

@coderabbitai

coderabbitai Bot commented Oct 30, 2026 •

Copy link
Copy Markdown
Contributor

Review in Change Stack →

Walkthrough

The changes add a check for paint retention across browser tabs and use it to update worktree recency before the retention hook checks active-worktree and budget conditions. The eviction selector now skips non-evictable worktrees before counting retained guests. Tests cover the updated eviction selection and retention after acquiring and releasing an automation visibility lease.

Priority: Normal

Severity of issue fixed: Medium

Merge Risk: Low · up to b7768

The retention test masks an initial mock eviction, leaving a bounded gap in lifecycle coverage. The fixture can be tightened as a follow-up.

Pre-merge checks: Passed 4 · Failed 1

Failed Failed checks: 1 warning

Check name Status Explanation Resolution
Linked Issues check Warning Issue #36235 requires the agent page to remain the same page while the agent works, including when commands are not flowing. The change gives claimed workspaces recency priority and excludes them from… Extend the retention behavior so an actively agent-driven page remains protected while the agent is working, independent of the short automation lease and the four-workspace budget. Add an automated test with more than four hidden live-tab …
Passed Passed checks: 4 passed
Check name Status Explanation
Title check Passed The title clearly and concisely describes the primary change: preventing agent browser tabs in background worktrees from reloading repeatedly.
Description check Passed The description includes the required sections, explains the user impact and mechanism, links issue #36235, documents testing and limitations, and addresses compatibility and performance. The unchecke…
Out of Scope Changes check Passed The changed production code updates browser guest recency, eviction-slot accounting, and the shared paint-retention veto. The tests cover those retention rules. These changes directly support issue #3…
Docstring Coverage Passed Docstring coverage is 100.00% which is sufficient. The required threshold is 80.00%. Docstring coverage is scoped to functions touched by this diff with cyclomatic complexity of at least 2. Documentat…
Full details: Description check

Explanation

The description includes the required sections, explains the user impact and mechanism, links issue #36235, documents testing and limitations, and addresses compatibility and performance. The unchecked full validation checklist does not prevent a pass because focused validation and CI coverage are described.

Full details: Out of Scope Changes check

Explanation

The changed production code updates browser guest recency, eviction-slot accounting, and the shared paint-retention veto. The tests cover those retention rules. These changes directly support issue #36235. The reported SSH reload path is not changed.

Full details: Docstring Coverage

Explanation

Docstring coverage is 100.00% which is sufficient. The required threshold is 80.00%. Docstring coverage is scoped to functions touched by this diff with cyclomatic complexity of at least 2. Documentation-only edits and deletions within surviving functions are included; unchanged diff context and fully deleted functions are excluded. Analyzed 2 functions across 5 files.

Full details: Linked Issues check

Explanation

Issue #36235 requires the agent page to remain the same page while the agent works, including when commands are not flowing. The change gives claimed workspaces recency priority and excludes them from the four-slot count, but the PR states that an idle claimed workspace can still be evicted when more than four hidden workspaces have live tabs. After the short claim expires, the code no longer marks that workspace as vetoed. The implementation therefore does not establish the required duration of retention in that case. The added test covers one claimed workspace outside the budget, not more than four idle claimed workspaces.

Resolution

Extend the retention behavior so an actively agent-driven page remains protected while the agent is working, independent of the short automation lease and the four-workspace budget. Add an automated test with more than four hidden live-tab workspaces and an idle interval after the lease expires.

Finishing Touches
📝 Generate docstrings
  • Commit to this branch
  • Create a new PR
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

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.

… ~10 s

The hidden-worktree guest budget ranked worktrees only by user activation, so a
worktree an agent drove in the background sorted last. Its automation lease was
its only veto, and when the lease lapsed 10 s after each command the retention
pass destroyed its webview; the next command loaded a fresh page.

Paint-retention claims (agent lease, phone driver, paired viewer) now count as
use when ordering the budget, and a worktree under a veto keeps its guests
without taking one of the four slots from the user's recent worktrees.
@AmoabaKelvin
AmoabaKelvin force-pushed the browser-bg-tab-reload-budget branch from 9ef525e to b7768b4 Compare October 30, 2026 05:04

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

Follow-up Comments

Nitpick Avoid the initial eviction without losing mounted-hook coverage.

69-69: Trivial · Maintainability & Code Quality · Quick win

Avoid the initial eviction without losing mounted-hook coverage.

renderRetention() enables the budget before acquiring the lease, so the initial effect can destroy AGENT. mockReset() clears that recorded call but does not restore the guest. Acquiring before rendering would avoid the eviction, but it would remove coverage for lease notifications delivered to an already-mounted hook.

Mount with the budget disabled, acquire and release the lease after mounting, then enable the budget with a rerender. The final pass should evict wt-a and should not evict AGENT. This preserves acquisition-notification coverage and exposes a regression that fails to update recency.

Suggested fix
-function renderRetention(): void {
-  renderHook(() => {
+function renderRetention(initialBudgetEnabled: boolean) {
+  return renderHook(
+    ({ budgetEnabled }: { budgetEnabled: boolean }) => {
       const [revision, setRevision] = useState(0)
       const recencyRef = useRef([...RECENT_HIDDEN])
       const mountedRef = useRef(new Set(SURFACES))
       useTerminalBrowserRetention({
-        browserGuestRetentionBudgetEnabled: true,
+        browserGuestRetentionBudgetEnabled: budgetEnabled,
         browserGuestRetentionRevision: revision,
         browserGuestWorktreeRecencyRef: recencyRef,
         mountedWorktreeIdsRef: mountedRef,
@@
         workspaceSurfaceIds: SURFACES,
         workspaceSurfaceIdSet: SURFACE_ID_SET
       })
-  })
+    },
+    { initialProps: { budgetEnabled: initialBudgetEnabled } }
+  )
 }
@@
-    renderRetention()
-    mocks.destroyWorktreeBrowserGuests.mockReset()
+    const retention = renderRetention(false)
 
     let token = ''
     act(() => {
       token = acquireBrowserAutomationVisibility(`page-${AGENT}`)
@@
     act(() => {
       releaseBrowserAutomationVisibility(token)
     })
+    act(() => {
+      retention.rerender({ budgetEnabled: true })
+    })

Open the source location


Review info
Run configuration
  • Source mode: Auto
  • Repository: Unavailable
  • Branch: Unavailable
  • File: No configuration file from this repository applied
  • Commit: Unavailable
  • Layers: Repository UI
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: b01d84ed-523d-4a37-a4ee-def7269b8649
Commits

Reviewing files that changed from the base of the PR and between 17828c7 and b7768b4.

Files selected for processing: 5
  • src/renderer/src/components/browser-pane/host-guest/browser-guest-eviction-veto.ts
  • src/renderer/src/components/browser-pane/host-guest/browser-guest-worktree-retention.test.ts
  • src/renderer/src/components/browser-pane/host-guest/browser-guest-worktree-retention.ts
  • src/renderer/src/components/use-terminal-browser-retention.test.tsx
  • src/renderer/src/components/use-terminal-browser-retention.ts

Included review availability: This review used your included allowance. Your plan provides up to 10 included reviews per hour; 1 remain after this review.

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.

Agent's browser tab in a background worktree reloads every ~10 s while another worktree is on screen

1 participant