Skip to content

feat(browser): embedded browser surface (S0 spike + S1 host) - #149

Merged
bubbleptr merged 8 commits into
mainfrom
feat/embedded-browser-host
Sep 2, 2026
Merged

bubbleptr merged 8 commits into
mainfrom
feat/embedded-browser-host

Conversation

@bubbleptr

Copy link
Copy Markdown
Owner

Summary

Lands S0 + S1 of .scratch/embedded-browser/PRD.md: the embedded browser is a built-in Session surface hosted by a main-process WebContentsView. URL only, no annotation layer yet (S2–S4 follow).

  • Host: WebContentsView owned by the main process; 8 browser_* commands routed through an explicit command table on pigui:invoke, events on pigui:browser-event. Nothing touches the utilityProcess or Runtime Gateway. Policy/lifecycle (browser-host.ts) is Electron-free and unit-tested.
  • Security: isolated persist:pigui-browser session, sandbox + contextIsolation, http/https only (checked at the command entry and in will-navigate, because main-process loadURL bypasses the latter), setWindowOpenHandler denies and loads in place, permissions denied, downloads cancelled.
  • Surface: browser registered in the inspector rail; address band with back/forward/reload/open-external; five states; per-Project URL memory in localStorage; empty state below the 1280px breakpoint (Sheet fallback cannot host a native view).
  • Overlay handling: any open DOM popup (Astryx Layer via Popover API toggle, Base UI portals via MutationObserver) swaps in a capturePage still and hides the native view, so tooltips/menus/dialogs are never covered.
  • Inspector width: max width is now "container minus Chat's 400px minimum" instead of 58vw (ADR-0028 amended), recomputed on resize.

Spike conclusions (recorded in the PRD)

  1. Placeholder-div → IPC → setBounds matches pixel-for-pixel; native view trails the DOM by ~one IPC round trip.
  2. Playwright reaches the WebContentsView contents via app.windows(), so E2E asserts the embedded page DOM directly.
  3. Isolated-world closed Shadow DOM overlay works on strict-CSP pages; the page cannot read it (it can remove the host element).

Deviations from the PRD

browser_open and browser_dispose dropped (no callers); title-updated / console events deferred to S2; renderer CSP left as a separate hygiene slice; ADR-0028's "Browser frozen by ADR-0007" line is handled by ADR-0029 in S4. Known limitations for ADR-0029 are listed in the PRD.

Test plan

  • bunx tsc --noEmit -p tsconfig.json
  • bun run test — 93 files, 833 tests
  • bun run test:e2e — 17 passed, including the new browser-surface.spec.ts (bounds follow the panel, popup swaps to a still, aborted-by-replacement load is not an error, Chat keeps ≥ 400px)
  • Manual on macOS: baidu.com / example.com load, rail tooltip visible over the page, ERR_ABORTED no longer shown

Refs #86.

Adds the Browser Session surface (S1 of the embedded-browser PRD): a native
WebContentsView owned by the Electron main process, driven by a `browser_*`
command group on the existing `pigui:invoke` main-process branch and a new
`pigui:browser-event` channel. The view never reaches the utilityProcess, so
the embedded page can never touch the Runtime Gateway MessagePort (ADR-0013).

- electron/browser-host.ts keeps the policy (URL allowlist, bounds clamping,
  the "no visibility without usable bounds" rule) behind a view seam so it is
  testable without Electron.
- The embedded session is isolated (`persist:pigui-browser`) with sandboxing,
  a deny-all permission handler, cancelled downloads, refused window.open and
  an http(s)-only navigation guard.
- The renderer contributes only a placeholder div; its rect is the single
  source of truth for the native view's bounds. Below 1280px the inspector is
  a Dialog portal, so no placeholder is rendered and no view is shown.
- Preview URLs are remembered per Project in localStorage; no new backend
  persistence.

Unfreezes Browser in the surface registry and registers BrowserSurface on the
/design page.
`session.fromPartition("persist:pigui-browser")` returns the same persistent
object for the life of the app, so configuring it inside createBrowserView()
stacked another `will-download` listener every time a disposed view was
recreated — e.g. switching to a Project with no remembered URL fires
`browser_dispose`, and the next navigate rebuilt the view. The permission
handlers replace each other, so only the download blocker accumulated.

`createBrowserSessionProvider` memoizes the configured session behind the same
seam as the rest of the host policy, so "configure exactly once" is unit
tested without Electron.
Five defects, each with a failing test first:

- A failed load commits Chromium's error page under the requested URL, so
  navigate()'s "already showing" short-circuit made Retry and re-entering the
  surface no-ops. The host now records a failed load (reported from
  did-fail-load, or seen directly when loadUrl rejects) and refuses to
  short-circuit while it stands.
- Only the instance that can host the view — docked, in Electron — sends it
  commands. The Sheet fallback unmounts after Base UI's exit transition, so
  its cleanup used to hide the view the docked instance had just shown.
- Events now carry a navigation id, bumped by every browser_navigate and
  browser_dispose. The renderer keeps the id its own last navigate answered
  with, so a page left behind by a Project switch can no longer write into the
  new Project's URL memory.
- invoke() strips Electron's "Error invoking remote method '<channel>': "
  wrapper, which was being read out verbatim in surface error states.
- Switching to a Project with no remembered URL no longer disposes the view
  (PRD section 6 keeps destruction for window close alone); hiding it is
  enough, and disposing also defeated the short-circuit above. PRD section 6
  records what that leaves behind.

Also routes browser commands on an explicit set of the eight the group
implements, so a future backend command starting with `browser_` is not
hijacked by prefix sniffing.
Nothing sends it since switching Projects stopped destroying the view: the
only teardown left is the window closing, and main calls the host's dispose()
directly for that. Removing the command takes the renderer wrapper and the
navigation-id bump on dispose with it, so the id now moves only on navigate —
which is the only thing the renderer matches events against anyway.

The test that covered re-applying bounds to a view recreated after dispose now
covers the path that still exists: bounds and visibility arriving before the
first navigate creates the view.
The native view paints above every DOM layer, so the inspector rail's own
tooltips — which open right against it — were unreadable. The PRD accepted
covered overlays as a v1 limitation; the rail makes it a per-hover collision,
so the v2 alternative lands now: while any overlay is open the placeholder
shows a `browser_capture` still of the page and the view steps aside.

Detecting "any overlay is open" needs two unrelated signals, and the probing
that established them is why this is not a one-liner:

- Astryx layers (Tooltip, Popover, Menu, Select) render their `[popover]`
  element inline in the trigger's own subtree, and opening calls
  `showPopover()` — no attribute changes, no node moves, so a MutationObserver
  never fires. The Popover API's `toggle` event is the only signal, and it
  does not bubble, so it has to be caught in the capture phase.
- Base UI overlays mount a `[data-base-ui-portal]` subtree on body and mark
  the live popup with `data-open`, which only the MutationObserver sees. The
  portal scope keeps inline `data-open` components (Collapsible in the chat
  column) from counting.

jsdom has no Popover API at all — Astryx falls back to plain visibility there
— so the unit tests drive real Astryx and Base UI components through the
fallback path, and the E2E asserts the real one against Chromium.

An overlay that closes before its still arrives keeps the live view rather
than flashing a frozen frame over a view that is already correct.
Typing www.baidu.com left the surface in its error state reading
"ERR_ABORTED (-3) loading 'https://www.baidu.com/'" while the page was on
screen and working: baidu navigates again before its first load finishes, so
Electron rejects the original `loadURL` with ERR_ABORTED. `did-fail-load`
already ignored that code, but the promise path added with the failed-load
retry fix did not, so every reject was recorded as a failure and shown.

ERR_ABORTED means "superseded", never "broken". The main-process adapter
resolves on it and lets the `did-navigate` event carry the state, and the host
repeats the check so no caller of the seam can be told otherwise. Every other
errno still fails as before.

The E2E gained a page that replaces itself while a subresource keeps its own
load pending, which is what actually produces the abort — verified by watching
it fail with both guards removed.
…s not need

The 58vw ceiling left the Browser surface too narrow on wide windows, and the
ratio never had a justification: it expressed "Chat needs at least this much"
as "the panel may have at most that much", which gets worse the wider the
window. Cursor's allocation is the right shape — Chat keeps a minimum width
and the panel takes the rest.

`sessionInspectorResizableBounds` now answers `max(340, available - 400)`,
where "available" is what Chat and the panel actually share (the split
container minus the resize handle's 17px footprint: `mx-2` plus the 1px
divider it draws, both measured rather than assumed).

The ceiling can no longer be resolved once at mount, so the split container
carries a ResizeObserver and the bounds are recomputed from its width.
`useResizable` clamps each drag against the current bounds but keeps a size it
already holds, so a window that shrank under the panel has its room handed
back explicitly. The ceiling floors rather than rounds: it exists to protect
Chat's minimum, and a fractional container width must not round the panel a
pixel over it — which the E2E boundary assertion caught.

Below 1280px the inspector is still a Sheet and is unaffected.
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