Skip to content

fix: fence browser recovery to attach inventory placements - #18910

Merged
nwparker merged 5 commits into
mainfrom
nwparker/fix-browser-attach-inventory-race
Sep 7, 2026
Merged

nwparker merged 5 commits into
mainfrom
nwparker/fix-browser-attach-inventory-race

Conversation

@nwparker

@nwparker nwparker commented Sep 5, 2026 •

Copy link
Copy Markdown
Contributor
Files Added Deleted Net
Test 2 $\color{#1a7f37}{\Huge{\mathbf{+}}}$​65 0 $\color{#1a7f37}{\Huge{\mathbf{+}}}$​65
Prod 2 $\color{#1a7f37}{\Huge{\mathbf{+}}}$​27 0 $\color{#1a7f37}{\Huge{\mathbf{+}}}$​27

ELI5

Orca can show web pages inside the app, and a paired device can be the thing that actually holds those pages open. When the part of Orca that keeps track of them restarted, it took a moment to re-check its list of open pages. If you opened a brand-new tab during that brief moment, the restarted bookkeeping did not know about that tab, assumed it had gone missing, and tried to build a replacement on top of the tab that was already sitting there. The desktop refused the contradictory instruction, and instead of losing just that one tab, every browser tab on that device went dead until you reconnected.

Now Orca writes down exactly which pages existed at the instant it began re-checking, and it only re-checks those. A tab you open while it is still catching up is left alone, because it is new and healthy and does not need rescuing. Opening a tab right after a restart no longer takes down the rest of your browser tabs.

What changed

After a paired runtime restarts, opening a new browser tab can shut down the entire client browser host. Attach publishes readiness and waits for existing-page adoption; recovery then scans the current page registry using the older attach inventory. It treats the newly created page as missing, retires its placement without closing the live guest, and sends another create at a higher generation. The desktop rejects that command with browser_host_page_replacement_requires_retirement, leaving all its browser tabs unavailable.

Capture page placements before publishing attach readiness and constrain recovery to those exact placements. Pages created or replaced during adoption retain their authority. The existing recovery path still handles pages that the captured inventory actually describes.

Validation: a real RPC regression creates a page while adoption is held; it fails before the fix because its placement is retired, then passes afterward. A second regression protects a replacement generation. All 51 focused adoption/recovery/negotiation tests pass, as do the 96 tests in the matching browser-network gate command. pnpm tc:node and targeted lint pass. The previously failing macOS paired cookie-survival journey passes three consecutive Electron runs, verifying the rendered post-restart page, surviving route cookie, and isolated default-session cookie jar. Double-restart, quit-survival, and ghost-close neighbors are still under investigation.

Reliability gate: browser-network-tunnel.bounded-fail-closed-route. Invariant: stale attach inventory must not retire a newer live page placement. Oracle: unchanged placement and active page, zero recovery commands for the new page, and rendered cookie survival after a real runtime restart. Diagnostics: the generation-replacement error and Playwright traces identify the original failure; both deterministic regressions preserve the race. The added snapshot is bounded by the existing 256-page registry cap and occurs once per attach; no polling, subprocess, or new protocol field is added. The host sends fewer erroneous recovery commands through the existing negotiated protocol, with no new behavior required of older clients. macOS paired/native execution is rendered-covered; Linux, Windows, SSH, WSL, folder workspaces, and mobile rendered coverage remain gaps. The logic compares page placements independently of execution-host paths and retains the existing folder/remote resolver.

Tradeoffs

Genuine negatives, not "none":

  • Skipping is fail-open, so one repair can be deferred. If a page's placement changes between the snapshot and the recovery scan, this attach leaves it alone. When that page was genuinely unhealthy, it is no longer repaired on this pass and stays unusable until the next attach — a reconnect or app restart — picks it up. Previously it would have been attempted immediately. The window is narrow and the user workaround is to close and reopen the tab.
  • The race is narrowed, not eliminated. A window remains between the client capturing its page inventory and the runtime taking its snapshot. On a reconnect or replacement attach, where the previous lease is still live, a tab created inside that message-hop window is present in the snapshot but absent from the inventory, so the original retire-and-recreate failure is still reachable. This is a pre-existing window that this change shrinks from "readiness plus the whole adoption pass" to "one message hop". Closing it fully requires changing the empty-inventory recreate rule and is deliberately not attempted here.
  • The fence is opt-in at the call site. pagePlacementsAtAttach is optional, so recovery runs unfenced if a future caller omits it. Only the attach handler calls it today and the parameter is documented, but this is a latent way to reintroduce the bug.
  • Slightly more work per attach. One Map of page id to placement is built on every attach, bounded by the existing 256-page registry cap, and released when the handler returns. No polling, no subprocess, no new protocol field.
  • Coverage is uneven across topologies. Rendered end-to-end proof exists only for macOS paired/native. Linux, Windows, SSH, WSL, folder workspaces, and mobile rely on unit-level coverage. The logic compares placements and never reads execution-host paths, so it is topology-independent by construction, but that is an argument rather than a measurement.

Visual proof

Not applicable, and not valuable here. The change has no rendered surface of its own; it suppresses an erroneous recovery command during attach. The only visible symptom is the absence of a failure — browser tabs staying alive after a restart — which a screenshot cannot distinguish from a broken build. The meaningful evidence is the paired cookie-survival Electron journey noted above plus the two deterministic regressions. Local Electron launches are paused for this task, so no app was launched.

Application behavior change: left open for user review; do not merge automatically.

@coderabbitai

coderabbitai Bot commented Sep 5, 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: Team

Run ID: 25cf25e8-53b9-4d32-aafa-af065407851b

📥 Commits

Reviewing files that changed from the base of the PR and between deebe05 and 633ddd5.

📒 Files selected for processing (4)
  • src/main/runtime/rpc/methods/browser-client-host-attach-adoption.test.ts
  • src/main/runtime/rpc/methods/browser-client-host.ts
  • src/main/runtime/runtime-browser-client-page-recovery.test.ts
  • src/main/runtime/runtime-browser-client-page-recovery.ts
🚧 Files skipped from review as they are similar to previous changes (4)
  • src/main/runtime/runtime-browser-client-page-recovery.ts
  • src/main/runtime/rpc/methods/browser-client-host.ts
  • src/main/runtime/rpc/methods/browser-client-host-attach-adoption.test.ts
  • src/main/runtime/runtime-browser-client-page-recovery.test.ts

Included review availability: Your plan provides up to 10 included reviews per hour; 4 remain after this review.


📝 Walkthrough

Walkthrough

The attach handler captures runtime page placements before adoption. It passes the captured placements to page recovery. Recovery skips pages whose current placement differs from the attach-time placement. Tests cover pages created after inventory capture and pages whose placements were replaced before recovery.

Merge Risk: ⚪ Minimal · up to e5685

Attach recovery now limits retirement to page placements observed when attachment began, preserving pages created or replaced during adoption. The targeted regressions cover both protected cases, with no remaining merge-blocking risk identified.

🚥 Pre-merge checks | ✅ 3 | ❌ 2

❌ Failed checks (2 warnings)

Check name Status Explanation Resolution
Description check ⚠️ Warning The description explains the problem, implementation, tradeoffs, testing, visual-proof rationale, and known limitations. However, it omits the required Linked Issue, Testing, Review, Agent skill upstr… Add all required template sections. Include a valid issue reference after "Fixes #", complete the Testing section and checklist, state the AI disclosure status when applicable, complete Review, Agent skill upstream boundary, Notes, and the …
Docstring Coverage ⚠️ Warning Docstring coverage is 50.00% which is insufficient. The required threshold is 80.00%. Docstring coverage is scoped to functions touched by this diff. Analyzed 2 functions across 4 files. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (3 passed)
Check name Status Explanation
Title check ✅ Passed The title clearly summarizes the primary change: limiting browser recovery to placements captured at attach time.
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.
Full details: Description check

Explanation

The description explains the problem, implementation, tradeoffs, testing, visual-proof rationale, and known limitations. However, it omits the required Linked Issue, Testing, Review, Agent skill upstream boundary, Notes, and Checklist sections.

Resolution

Add all required template sections. Include a valid issue reference after "Fixes #", complete the Testing section and checklist, state the AI disclosure status when applicable, complete Review, Agent skill upstream boundary, Notes, and the remaining Checklist items.

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

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

✅ No new issues found.

Reviewed changes

The fix fences browser recovery to the page placements captured at attach time, so a page created or replaced during adoption can no longer be mistaken for a missing guest and retired out from under a live tab.

  • Placement snapshot at attach — browser-client-host.ts captures pagePlacementsAtAttach from the page registry before registry.attach()/emit ready and threads it into recovery.
  • Recovery fence — recoverUnavailableRuntimeBrowserClientPages gains an optional pagePlacementsAtAttach; the filter now skips any page absent from the snapshot or whose placement changed since capture (via sameRuntimeBrowserPlacement).
  • Two deterministic regressions — a real-RPC test creating a page while adoption is held, and a unit test for a replacement placed after capture, both asserting no recovery commands and unchanged placement.

The fence is correctly conservative: the optional parameter leaves existing callers/tests unchanged, restored pages (whose sentinel placement is stable across capture) still reach recovery, and adopted pages remain doubly protected by adoptedPageIds. No wire or protocol change, and the snapshot is bounded by the existing registry cap. Focused test files pass locally (29 tests).

Pullfrog  | View workflow run | Using DeepSeek Pro (free via Pullfrog for OSS) | 𝕏

…stic

Extract the placement check into isPlacedAsObservedAtAttach so the recovery
filter stays a flat list of named predicates, and document that omitting
pagePlacementsAtAttach recovers against unfenced live state.

Replace the 30-microtask drain in the post-attach regression with the handler's
own completion: attach only settles after recovery returns, so awaiting the
dispatch orders the assertions instead of guessing at a microtask count.

Verified by forcing the fence open: both regressions fail (the post-attach one
in 60ms on a retired placement) and the other 27 still pass.
The barrier ran inline, so a waitFor timeout or the placement guard left the
attach handler parked on a promise nothing awaited. Hoist it into settleAttach
and call it from a finally as well; cleanup is guarded and the dispatch promise
is already settled, so the second call is a no-op.

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

✅ No new issues found.

Reviewed changes

The follow-up commit hardens the regression test's determinism and names the recovery fence it exercises — no behavior change to the production path.

  • Replaced the microtask drain with a real barrier — the real-RPC regression now waits on cleanup() + await rig.dispatch, which only settles once the attach handler (and thus recovery) has returned, instead of guessing with 30 drained microtasks.
  • Extracted isPlacedAsObservedAtAttach — the inline placement-match clause is now a named helper with a JSDoc explaining why readiness-before-recovery requires fencing pages created or replaced during adoption. The observed !== undefined check is behavior-equivalent to the prior truthiness check, since a placement is always a non-null object.

Pullfrog  | View workflow run | Using DeepSeek Pro (free via Pullfrog for OSS) | 𝕏

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

✅ No new issues found.

Reviewed changes

The follow-up commit makes the regression test's settle barrier run even on early failure — no production code changes.

  • Restored settle-on-failure for the regression test — the cleanup() + await rig.dispatch barrier is now a settleAttach() helper invoked in a finally, so the streaming attach handler still settles if an early step (e.g. the vi.waitFor on resolveExecutionHostKey) fails, instead of hanging the test process. Calling it twice is safe because the handler's cleanup() is idempotent.

Pullfrog  | View workflow run | Using DeepSeek Pro (free via Pullfrog for OSS) | 𝕏

@coderabbitai

coderabbitai Bot commented Sep 7, 2026

Copy link
Copy Markdown
Contributor

Note

GitHub couldn't provide a complete incremental comparison for this pull request, so CodeRabbit is performing a full review instead. This review may take a little longer.

@nwparker
nwparker merged commit e7563c6 into main Sep 7, 2026
34 checks passed
enrick-dev pushed a commit to enrick-dev/orca that referenced this pull request Oct 15, 2026
…18910)

* fix: fence browser recovery to attach inventory placements

* refactor: name the attach-inventory fence and make its test deterministic

Extract the placement check into isPlacedAsObservedAtAttach so the recovery
filter stays a flat list of named predicates, and document that omitting
pagePlacementsAtAttach recovers against unfenced live state.

Replace the 30-microtask drain in the post-attach regression with the handler's
own completion: attach only settles after recovery returns, so awaiting the
dispatch orders the assertions instead of guessing at a microtask count.

Verified by forcing the fence open: both regressions fail (the post-attach one
in 60ms on a retired placement) and the other 27 still pass.

* test: settle the attach handler even when the regression fails early

The barrier ran inline, so a waitFor timeout or the placement guard left the
attach handler parked on a promise nothing awaited. Hoist it into settleAttach
and call it from a finally as well; cleanup is guarded and the dispatch promise
is already settled, so the second call is a no-op.
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