Skip to content

fix(browser): a tab's phone identity follows the viewport actually applied - #23812

Merged
brennanb2025 merged 3 commits into
mainfrom
brennanb2025/browser-identity-applied-viewport
Sep 29, 2026
Merged

brennanb2025 merged 3 commits into
mainfrom
brennanb2025/browser-identity-applied-viewport

Conversation

@brennanb2025

@brennanb2025 brennanb2025 commented Sep 29, 2026 •

Copy link
Copy Markdown
Contributor

What this fixes. In Orca's built-in browser, a tab with a phone viewport preset (for example "Mobile M") could claim to be an iPhone while showing a desktop-sized page, or claim to be desktop Chrome while showing a phone-sized page. This happens only when Chromium rejects the command that resizes the page's emulated screen (the "device metrics" step). #23718 split the preset switch into independent steps so a failed step no longer strands the old identity, but its identity step followed the preset the user asked for, not the one that actually took effect. Greptile flagged this as a P1 on #23718 (comment).

Scope. Only the embedded browser's per-tab viewport presets (the Viewport Size menu, and the page-load reapply of a stored preset). Only when the device-metrics step fails, or the tab's debugger detaches (which clears Chromium's emulation). Normal preset switching, Google sign-in handling, native vs clean identity mode, popups, workers and the network-level user agent are unchanged. The touch-emulation payload is untouched (that is #22846). A post-merge audit of #23718 could not trigger the metrics failure in the running app, so this is hardening of a real code path rather than a fix for a commonly seen bug.

ELI5

When you pick a phone size for a browser tab, Orca does two things: it shrinks the page to a phone screen, and it tells websites "I'm an iPhone". If shrinking failed, Orca still said "I'm an iPhone" on a full-size page, which confuses sites and bot checks. Now Orca only says "I'm an iPhone" while the page really is phone-sized.

What Changed

User-facing: a tab's identity (iPhone vs your normal browser) now always matches the viewport you can actually see. If a phone preset fails to apply, the tab stays a normal desktop browser until the preset applies (the next page load re-sends it). If leaving a phone preset fails, the tab keeps presenting as an iPhone for as long as the phone viewport is still on screen, instead of claiming desktop on a phone-sized page.

Mechanism: the per-tab viewport record in the main process now holds two things, the preset that was requested and the device-metrics override Chromium actually accepted (requested and applied in viewportPresetByTabId). applied only moves after Emulation.setDeviceMetricsOverride / clearDeviceMetricsOverride succeeds for the same guest, and it is recorded right after that step, before touch emulation runs, so a debugger detach arriving during the touch step cannot be overwritten. It is dropped when the tab's debugger detaches, because a detach clears Chromium's emulation. The identity owner (resolveBrowserTabIdentity, via hasMobileViewportApplied) reads applied; host-side wheel panning keeps reading requested, as before. The network request hook already follows the user-agent override Orca actually wrote, so it now follows the applied viewport too, without any change of its own.

A side effect of the same rule: when presets are toggled rapidly, each applied viewport now gets its matching identity in turn (a phone viewport briefly presents as a phone before the next switch lands), where before the identity jumped straight to the last request even while an older viewport was still on screen.

Why

This holds the invariant "the identity a tab presents matches the viewport it is showing" by construction, in both directions, instead of adding a guard at the one failure site. Identity is derived from state Orca already has to track (what Chromium accepted), so there is no separate flag to keep in sync, and the record answers the next question too (which exact override is applied, not just "mobile or not").

Alternatives considered: (1) a standalone "mobile identity allowed" boolean set when metrics succeed. It answers only today's question and needs its own reset rules; the applied override carries the same fact plus the size. (2) Rolling back or retrying from the renderer when setViewportOverride returns false. The next page load already re-sends the stored preset, which re-applies metrics and then the identity, so retry adds UI state for a failure that could not be triggered live; left as a follow-up below. (3) Skipping the identity step when metrics fail. That breaks the phone to desktop failure case, where the identity must stay mobile because the phone viewport is still on screen.

The #23718 test "still presents the requested mobile identity when device metrics fail" asserted the contradictory state (iPhone identity on a desktop viewport). It is replaced by tests that assert the opposite, plus tests for the reverse direction, a debugger detach, and a detach that lands in the middle of a preset switch. The #23718 ownership test for rapid toggles now expects the per-step identity writes described above.

Linked Issue

Follow-up to #23718 (Greptile P1 on that PR). No separate issue.

Visual Proof

Live check on a second Mac: a production build of this branch, driven through the same two calls the Viewport Size menu makes, observed from the main process and an HTTPS echo server. Every step matched the #23718 merge build exactly (same page UA, navigator.userAgentData, request UA and sec-ch-ua* headers, iframe, dedicated worker and service worker) for none, Laptop L, Mobile M, Mobile M to Laptop L, Laptop L to none and Mobile M to none, in both clean and native identity modes, and for Google sign-in under each preset in clean mode (Firefox, no hints). The app logged 0 device-metrics failures and 0 identity-write failures; the only warnings were the known touch-emulation rejections that #22846 fixes.

Preset Page reports
Mobile M iPhone UA, userAgentData mobile / iOS with brands, 375 px, DPR 2
Mobile M to Laptop L desktop Chrome UA, userAgentData desktop / macOS with brands, 1440 px
Back to no preset desktop Chrome UA, userAgentData desktop / macOS with brands

Mobile M (the page prints its own navigator values):

Mobile M: iPhone identity on the phone viewport

Mobile M, then Laptop L:

Mobile M then Laptop L: desktop identity restored

Laptop L:

Laptop L: desktop identity with client hints

No preset:

No preset: desktop identity

These show normal switching is unchanged. The changed path (device metrics failing) cannot be produced in the running app, so it has no before/after screenshot; the unit tests above carry it.

Testing

  • I manually tested these changes locally

  • Automated tests added/updated, or explained why not below

  • New unit tests in browser-manager-tab-identity.test.ts (5 cases): a phone preset whose device metrics fail keeps the process identity on the page, in requests, and after the next navigation, and becomes mobile once the preset applies; phone to desktop and phone to no preset with metrics failing keep the mobile identity while the phone viewport stands, including after the next navigation; after a debugger detach the next navigation does not reinstall the mobile identity; a detach during the touch step of a phone switch does not leave a mobile identity to reinstall.

  • Ablation: identity reading the requested preset again fails all 5; removing only the detach handling fails both detach tests; recording applied after the touch step again fails only the mid-switch detach test.

  • All 194 unit test files under src/main/browser plus src/main/ipc/browser* pass (1989 tests). pnpm tc:node passes; oxlint is clean on the changed files; the branch merges cleanly with fix(browser): restore hover after leaving a mobile viewport preset #22846.

  • Live no-regression check on a second Mac (production build of this branch, hidden window, isolated profile, clean and native identity modes): see Visual Proof. The metrics failure itself cannot be triggered in the running app, so the unit tests carry it.

Review

Follow-ups, not in this PR (all predate it):

  • The renderer ignores a false return from setViewportOverride (Viewport Size menu and the page-load reapply), so a failed preset still shows as selected, and host wheel panning follows it, until the next page load re-applies it. The identity is now consistent in that state; only the menu selection is misleading.
  • If clearing a phone viewport succeeds but the identity write itself fails, the phone user agent stays until the next cross-document navigation clears it.
  • The agent orca browser viewport --mobile command and the phone screencast set device metrics directly, outside presets, so the tab's identity does not follow them (it did not before either).
  • A main-process crash when a preset is applied to a tab whose page renderer has crashed (seen in the fix(browser): give a tab's identity one owner so viewport presets stop dropping client hints #23718 audit).

Agent skill upstream boundary

  • Not applicable, or this change follows docs/reference/agent-skill-sharing-upstream-boundary.md and copies or mechanically translates no upstream skill-installer source, tests, fixtures, registry entries, path tables, comments, or documentation.

Notes

Security: no new surface; this only narrows when the mobile user agent is sent. Cross-platform: main-process state only, no platform-specific code. SSH/remote: the embedded browser runs locally; no wire changes. Mobile app: not affected. Backwards compatibility: in-memory state only, nothing persisted. Performance: one extra map write per successful metrics step.

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)

@brennanb2025
brennanb2025 marked this pull request as ready for review September 29, 2026 07:36
@brennanb2025

Copy link
Copy Markdown
Contributor Author

Status: ready for review. Not merged; auto-merge is off.

What this PR does. A built-in browser tab now presents an iPhone identity only while a phone viewport is actually applied. That holds in both directions when Chromium rejects the viewport resize, and after a debugger detach. This closes the Greptile P1 raised on #23718.

Review loops

  • Start-of-review readiness checklist: pass. It found one minor issue: the applied viewport was recorded after the touch step, so a debugger detach landing in that window could be overwritten.
  • Review loop 1: found the same issue, rated medium, and fixed it in bee8239714b. The applied viewport is now recorded straight after the metrics step, with a new test.
  • Review loop 2: clean. It noted a stale PR description, now corrected. A comment was also narrowed to preset writes in da29e492c64.
  • End-of-review readiness checklist: pass, no findings.
  • Architecture challenge: not needed. There were two loops and no repeated class of issue.

Verification

  • Unit tests: 5 new cases. Each fails when its part of the fix is reverted.
  • Wider suite: 194 browser unit test files pass (1989 tests).
  • Typecheck: tc:node passes.
  • Merge checks: merges cleanly with main and with fix(browser): restore hover after leaving a mobile viewport preset #22846.
  • CI: green on da29e492c64: 16 pass, 15 skipped, 0 failing.
  • Live check on a second Mac: production build of this branch, clean and native identity modes. Normal preset switching matches fix(browser): give a tab's identity one owner so viewport presets stop dropping client hints #23718's build exactly: page and request user agent, navigator.userAgentData, client hints, iframe, worker and service worker. Google sign-in stays Firefox in clean mode. 0 device-metrics failures and 0 identity-write failures. Screenshots are embedded in the PR body.
  • Not verified live: the device-metrics failure itself can't be triggered in the running app, so the unit tests carry that path.

Unexpected behaviour for users: none introduced by this PR.

Follow-ups (all predate this PR):

  • The Viewport Size menu keeps showing a failed preset as selected until the next page load re-applies it.
  • If clearing a phone viewport succeeds but the identity write fails, the phone user agent stays until the next navigation.
  • Agent viewport commands and the phone screencast set the viewport outside presets, so the tab's identity doesn't follow them.
  • The main process crashes when a preset is applied to a tab whose page renderer has crashed.

@greptile-apps

greptile-apps Bot commented Sep 29, 2026 •

Copy link
Copy Markdown

RetriggerConfidence Score: 4/5

[Medium risk] Fixes viewport preset state tracking for mobile device emulation.

The PR should not merge until an in-flight metrics result cannot restore mobile identity state after debugger detach.

Findings

  1. P1 Detach can restore stale identity ▶

Summary

The PR separates requested viewport presets from successfully applied device metrics, derives tab identity from the applied metrics, and clears that state on debugger detach.

  • Adds tests for failed metrics writes, detach, and rapid preset switches.
  • An in-flight metrics result can still restore applied state after a detach.

Reviews (1) · Last reviewed commit: "chore(browser): scope the applied-viewpo..."

Comment on lines +164 to +165
if (metricsApplied) {
this.recordAppliedViewportOverride(browserTabId, webContentsId, override)

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P1 Detach can restore stale identity

If Chromium accepts a mobile metrics command and the debugger detaches before this awaiting operation resumes, the detach handler clears applied, but this write sets it back to mobile. Detaching has cleared the metrics, so the next navigation can reinstall an iPhone identity on a desktop viewport. The result needs to be guarded against a detach during the command.

@coderabbitai

coderabbitai Bot commented Sep 29, 2026

Copy link
Copy Markdown
Contributor

Review in Change Stack →

Navigate logical layers of code changes, visualize relationships, and explore their blast radius.

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Repository UI

Review profile: CHILL

Plan: Advanced

Run ID: 0b67a143-3136-4338-b074-4aeef184ada4

📥 Commits

Reviewing files that changed from the base of the PR and between 5560e53 and da29e49.

📒 Files selected for processing (8)
  • src/main/browser/browser-manager-bindings.ts
  • src/main/browser/browser-manager-guest-policy.ts
  • src/main/browser/browser-manager-navigation.ts
  • src/main/browser/browser-manager-state.ts
  • src/main/browser/browser-manager-tab-identity.test.ts
  • src/main/browser/browser-manager-viewport-ownership.test.ts
  • src/main/browser/browser-manager-viewport-scroll-state.ts
  • src/main/browser/browser-manager-viewport.ts

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


📝 Walkthrough

Walkthrough

Viewport preset state now records requested and successfully applied overrides separately. Mobile identity checks use the applied viewport state. Debugger detachment clears the recorded user-agent override and, when a tab is mapped, its applied viewport override. Tests cover failed preset changes, retries, navigation, and debugger detachment.

Priority: ⬇️ Low

Merge Risk: ⚪ Minimal · up to da29e

No merge-blocking issue is established for viewport preset identity; the change is mergeable after normal checks.

Security Architecture Review

Security architecture risk: 🔵 Low · up to da29e

The change is limited to embedded-browser tabs and reduces the chance that a site sees a phone identity with a desktop viewport, or the reverse. The review found no introduced security concern, though behavior under unusual Chromium failures remains difficult to verify.

Retained concerns
No architecture-level concerns identified.

Security review details

Security Blast Radius

  • inferred — The changed identity decision is bounded to the tab's directly mapped guest: a popup or replacement guest does not acquire another guest's applied mobile state through this lookup.

Trust Boundaries and Controls

  • observed — Navigation supplies a URL to identity resolution, but mobile selection also requires guest-owned applied viewport state. The inspected change does not let the navigated page set that state.

Resilience and Maintainability Implications

  • observed — Tests cover a rejected metrics command followed by navigation and retry, and a debugger detach followed by navigation. They support the intended recovery behavior but do not establish every Chromium failure mode.

Hardening Proposals

  • proposed — If a reproducible engine-level test becomes available, verify that a successful device-metrics response means metrics took effect and that detach cannot leave a mobile identity active after emulation clears.
🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 25.00% which is insufficient. The required threshold is 80.00%. Docstring coverage is scoped to functions touched by this diff. Analyzed 4 functions across 8 files. 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 the main change: the tab identity follows the viewport that was actually applied.
Description check ✅ Passed The description is complete and relevant. It covers the user impact, implementation, rationale, linked follow-up issue, visual proof, testing, scope, risks, and checklist items. The linked issue is re…
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.
  • Fix all pre-merge checks with AI
✨ Finishing Touches 💡 1
📝 Generate docstrings 💡
  • Commit to this branch
  • Create a new PR

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 — a tab's phone identity now derives from the device-metrics override Chromium actually accepted, not the preset the user asked for.

  • Dual viewport record: viewportPresetByTabId entries now carry requested (written before CDP, drives host wheel panning) and applied (written only after a successful metrics/clear step, drives identity). recordAppliedViewportOverride is fenced on guestWebContentsId, so a stale or replacement guest cannot write onto the new owner's record.
  • Identity follows applied: hasMobileViewportPreset becomes hasMobileViewportApplied, so resolveGuestTabIdentity (and the page-load reapply) matches the viewport on screen in both directions — a failed phone apply stays desktop, a failed phone clear stays mobile while the phone viewport stands.
  • Detach handling: trackDebuggerDetachForUserAgentOverride becomes trackDebuggerDetachForCdpOverrides and clears applied too, since a detach clears device metrics along with the UA override. applied is recorded before the touch await, which makes the write atomic relative to the detach handler and closes the mid-switch detach case.
  • Tests: the contradictory #23718 case is replaced by five identity cases (failed apply both directions, detach, mid-apply detach) plus updated ownership-shape/ordering expectations.

I read the complete diff and the surrounding implementation (browser-manager-viewport.ts, browser-manager-navigation.ts, browser-manager-state.ts, browser-manager-registration.ts, browser-guest-wheel-zoom.ts), confirmed the only readers of the changed state are the identity resolver and isViewportPresetActive, and checked the invalidation sites (guest replacement/unregister delete the record). I also ran the two affected test files (41 passing) and ablated the fix by reverting hasMobileViewportApplied to read requested, which fails all five new identity cases — the tests genuinely pin the behavior rather than merely passing.

The known follow-ups (renderer ignores a false return so the menu can still show a failed preset; an identity-write failure after a successful clear; direct-metrics paths like orca browser viewport --mobile) are pre-existing and explicitly out of scope. Approval is conditional on nothing else landing; if commits do land, treat these line references as potentially stale.

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

@brennanb2025
brennanb2025 merged commit 0fd098e into main Sep 29, 2026
58 checks passed
brennanb2025 added a commit that referenced this pull request Sep 29, 2026
…3789, #23812, #23852) into brennanb2025/codex-home-single-writer

#23744 moves every agent's folder pre-trust into the PTY spawn hook (and
the structured Codex chat create), removing Codex's launch-prep and
resume-prep trust writes. markCodexProjectTrusted now takes the config
files to write.

Conflicts:
- agent-trust-presets.ts: #23744's configFiles signature and nested lane
  acquisition, with this branch's lane rationale (hook installs read and
  rewrite across awaits); main's "capture->restore window" wording
  described the grant snapshot this branch removed.
- agent-trust-write-deadline.ts: the Codex writer waits only on
  managed-home installs' inline grant; the real-home background approval
  holds no lane. #23744's SSH/SFTP writer is gone, so it is no longer
  named; its 1.5 s short deadline is kept.
- codex-trust-config-concurrent-launch.test.ts: this branch's tests
  (a launch's write lands while a grant session runs; the renamed
  sibling-failure case) with #23744's new call signature.

Outside the conflicts: codex-launch-during-real-home-grant.test.ts passed
launchAgent/workspacePath into launch prep, which #23744 removed; its
launch helper now runs launch prep and then applyAgentWorkspaceTrust, as
the spawn builder does, so it still proves a launch and its project trust
land while the real-home approval hangs.
brennanb2025 added a commit that referenced this pull request Sep 30, 2026
…the CDP override

#23812's detach test called Orca's detach handler without clearing the fake's override, which
passed only while the harness ignored the real header UA. Also guard requestIdentity with ?. now
that the resolver can return nothing.
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