Skip to content

feat(browser): rebuild agent browser tools around offscreen tabs, locators, and real waits - #53861

Closed
Hona wants to merge 5 commits into
anomalyco:devfrom
Hona:browser-tools-rebuild
Closed

Hona wants to merge 5 commits into
anomalyco:devfrom
Hona:browser-tools-rebuild

Conversation

@Hona

@Hona Hona commented Oct 8, 2026 •

Copy link
Copy Markdown
Member

Agents used the desktop browser in 96 recorded sessions (2,226 Code Mode calls, 7,967 browser sub-calls), and 29% of those sub-calls failed. Most failures had a few root causes:

  • Hidden tabs did not render. With the Review pane hidden or the window minimized, screenshot failed (57%), click and hover threw a bare UnknownVizError (43%), key presses were lost, and requestAnimationFrame never ran.
  • There was no way to wait. Code Mode has no timers, so agents used wait({ text: "zzzz-never", timeout }) as a sleep: 2,012 calls failed at once on a missing condition and were hidden by .catch(() => {}).
  • Actions took snapshot refs only. find searched an accessibility tree 8 levels deep, so agents fell back to evaluate for clicks, typing, waiting, and reading (3,456 calls).
  • Small things added up.
    • Agents guessed parameter names (expression, query, requestID), and nothing rejected them.
    • file:// was refused, so agents started ad-hoc servers.
    • Every page was 480 px wide.
    • Subagents got browser.disconnected.

This PR rebuilds the tool surface around three guarantees: agent tabs always render, every element parameter takes one locator, and waiting returns a result.

Agent tabs render offscreen

flowchart LR
  agent[tools.browser.*] -->|CDP| osr[Offscreen WebContents<br/>agent tab]
  osr -->|paint frames| presenter[Presenter view<br/>in the Review pane]
  presenter -->|pointer, keys, IME, edit commands| osr
  user[User's own tabs] --> native[Native WebContentsView<br/>unchanged]
Loading
  • Tabs that tabs.open creates are offscreen pages, so painting, layout, input, and requestAnimationFrame do not depend on the pane or the window. On Electron 44, offscreen rendering was the only setup that passed visibility, animation frames, click, typing, and capture with the window normal, minimized, and hidden.
  • A presenter WebContentsView (presenter.ts) takes the page's place in the embed system. It pulls the newest frame as JPEG, shrunk to the pane's pixel size, through a protocol handler in its own partition, and forwards the user's input (presenter-input.ts). The page keeps its own size (1280x800, or a pinned viewport) and the presenter scales it to fit, so watching never changes the agent's layout.
  • Encoding runs on the main thread, so frames take at most a quarter of it. Measured on Electron 44: a 2560x1600 frame costs about 32 ms as JPEG, and 16 ms shrunk to 1280 wide first.
  • The desktop wires its RPC port, theme, and storage events only to app windows (getMainWindows), so offscreen pages that load web content never receive the privileged port.
  • Painting runs at 30 fps while the tab is watched or an agent call runs, and stops when the tab is idle; scripts keep running. A link that opens a new tab becomes another agent tab, up to 20 tabs.
  • Agent tabs keep their owner, key, and pinned viewport across an app restart. Focus goes to offscreen pages only on Windows; on macOS and Linux focusing them would show their hidden window, so focus emulation stands in.
  • The user's own tabs stay native views. When a user's tab is off screen, an action now fails with code tab_hidden and points to tabs.open, instead of UnknownVizError or a hang.

One locator for every element

target, from, and to accept refs, CSS, text=, role=…[name=…], label=, placeholder=, testid=, and xpath=, chained with >> and nth= (locator.ts). Locators resolve in the page through open shadow roots (page.ts). Before an action runs, the element must be attached, visible, stable, enabled, and not covered.

const tab = await tools.browser.tabs.open({ path: "dist/index.html", key: "app", viewport: { width: 1280, height: 800 } })
const r = await tools.browser.click({ tabID: tab.id, target: 'role=button[name="Sign in"]' })
// r.changed → { navigated, url, title, dialog, errors, matches }
await tools.browser.fill({ tabID: tab.id, target: "label=Plan", value: "Pro" }) // select by visible label
const { met } = await tools.browser.wait({ tabID: tab.id, text: "Welcome", timeoutMs: 20000 })
await tools.browser.screenshot({ tabID: tab.id, path: "pr/after.png" }) // works with the app minimized

When nothing matches, the error lists visible elements that look related:

[browser.not_found] Nothing matched text=Show detials after 5000 ms. Visible elements that look related:
button "Show details", textbox "Email", ... Use browser.find or browser.snapshot to see the page.

Tool surface

Before After
wait({condition, text, timeoutMs ≤ 30s}) throws on timeout wait({ load | text | gone | target+state | url | script | idle }) → { met, elapsedMs, observed }; no condition means a delay, the way to pause in Code Mode; timeout is accepted as timeoutMs
click/hover/fill({ ref }), plus select, check, fill_form click/hover/drag/fill/type/press/scroll take target; fill sets any control; type sends real keystrokes
find (8-level accessibility search), evaluate for reading find searches the whole DOM with refs, boxes, styles, and attributes; new read (text or Markdown, paginated)
evaluate({ script }) takes expressions only Expressions, statements with return, or functions with args; each call has its own scope; saveTo for large results
refs cleared by every snapshot refs last until their element leaves the page
file:// refused, 480 px pages tabs.open/navigate({ path }) serves the page on loopback: a workspace page with its folder (no dotfiles, parents, or symlinks out), any other HTML page alone; stops with the plugin; viewport and emulate
trace.*, cpu.*, 5 heap.* tools profile.start/stop with the analysis inline, and heap.snapshot with compareTo
none watch, storage, addInitScript, tabs.open({ key }) for reuse
  • console and network.list keep entries across navigations and accept since cursors. network.get returns WebSocket frames. reload returns the errors it caused.
  • Subagent sessions use the browser of the session that started them. Tool visibility still comes from the permissions a subagent copies when it starts, so one started before the parent's browser attached does not see the tools.
  • screenshot.path, files.get.path, and evaluate.saveTo must name a path inside the workspace (checked through symlinks) and never replace a file the browser did not save, because the plugin cannot ask for edit permission.
  • A malformed CSS selector, XPath, or /regex/ fails at once with invalid instead of waiting out the timeout.
  • Each request's timeout follows the action: wait and watch can run longer than the old fixed 60 seconds.
  • preview reports opened, or the reason it could not open. An HTML preview opens as an agent tab and returns its tabID; previewing the same file again reloads it.
  • The attachment protocol moves to version 5: a desktop and server on different releases report the browser as unsupported instead of exchanging operations the other cannot read.

Hidden-window capture

Before After
Screenshot needs a visible tab. Call browser.tabs.focus… (122 failures) Captured while the app window was minimized: minimized

@Hona
Hona requested a review from Brendonovich as a code owner October 8, 2026 05:01
Copilot AI balanced review requested due to automatic review settings October 8, 2026 05:01

Copilot AI 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.

Copilot was unable to review this pull request because the user who requested the review has reached their quota limit.

Hona added 3 commits October 9, 2026 13:09
One locator grammar for every element parameter, wait as a result instead of an error, new read/type/watch/emulate/storage/addInitScript/handoff tools, local files served to tabs by path, screenshots and large results saved to server paths, subagents borrowing the parent's browser, and per-action deadlines. Protocol version 5.
…new browser tools

Agent tabs are offscreen pages that paint, lay out, and take input whether or not the Review pane or window is visible; a presenter view streams their frames into the pane and forwards the user's input. Idle unwatched tabs stop painting. The page driver resolves locators in the page (shadow DOM included), waits for actionable elements, sends trusted input, and reports what each action changed.
@Hona
Hona force-pushed the browser-tools-rebuild branch from b99d81d to 7fb2d4c Compare October 9, 2026 03:09
Hona added 2 commits October 9, 2026 14:22
- Wire the desktop RPC port, theme and storage events only to app windows,
  not to offscreen agent tabs that load web content.
- Keep browser saves inside the workspace, through symlinks, and never
  replace a file the browser did not save.
- Serve local files without following symlinks out of the folder.
- Fail at once on malformed CSS, XPath and regex; report a wait script's
  last error; take gone as a locator; honor frameID with target.
- Reload a re-run HTML preview; SVG stays a media preview.
- Keep agent tabs at their own size while watched, scaled to fit.
- Bound frame encoding to a quarter of the main thread and shrink frames
  to the pane's pixel size; paint at 30 fps.
- Run macOS edit chords as commands in agent tabs, send selectAll with
  Cmd+A, and pass modifier keys and the Space key through.
- Cap tabs opened by pages, drop the unused Command.generation, and
  format with prettier.
- Save agent tabs' owner, key and viewport, so they come back as agent
  tabs after a restart, and reuse a restored tab for tabs.open({ key }).
- Serve a workspace page with its folder only outside dot-folders; any
  other HTML or SVG page is served alone, and other files are refused.
- Refuse saves through broken symlinks or onto a saved file that became
  a link.
- wait: a removed ref is detached, state needs target or text, text
  honors attached and detached, and enabled treats a removed element as
  not enabled.
- Report the tab's own viewport after a screenshot at another size, and
  size captures with the viewport's device scale.
- Do not treat button, hidden or file inputs as editable.
- Focus offscreen tabs only on Windows: elsewhere it shows their window.
- Size the presenter canvas before its first frame, and fetch the frame
  again when the pane grows.
- Give type a deadline that covers its text, end watch's last sleep with
  its duration, and key previews by their served URL.
- Do not test idle's reset in happy-dom, which holds observer callbacks
  weakly; Electron covers it.
@github-actions

github-actions Bot commented Nov 8, 2026

Copy link
Copy Markdown
Contributor

Automated PR Cleanup

Thank you for contributing to opencode.

Due to the high volume of PRs from users and AI agents, we periodically close older PRs using automated criteria so maintainers can focus review time on the most active and community-supported contributions.

This PR was closed because it matched the following cleanup criteria:

  • The PR was created more than 1 month ago
  • The PR had fewer than 2 positive reactions
  • Positive reactions are counted as thumbs-up, heart, celebration, or rocket reactions on the PR

PRs created within the last month are not affected by this cleanup.

If you believe this PR was closed incorrectly, or if you are still actively working on it, please leave a comment explaining why it should be reopened. A maintainer can review and reopen it if appropriate.

Thanks again for taking the time to contribute.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants