Skip to content

feat(selfhost): render_page through an optional Chrome sidecar (#292) - #339

Merged
Madraka merged 1 commit into
mainfrom
claude/selfhost-chrome-292
Nov 29, 2026
Merged

Madraka merged 1 commit into
mainfrom
claude/selfhost-chrome-292

Conversation

@Madraka

@Madraka Madraka commented Nov 29, 2026

Copy link
Copy Markdown
Contributor

render_page now works on self-host through an optional Chrome sidecar (RFC-0047 §6). Before this, the image carried no browser, so the tool reported itself unavailable.

Design

  • Config. CLOUDHAL_BROWSER_URL names Chrome's DevTools port, for example http://chrome:9222. The generated workerd config turns it into (name = "browser", external = (address = "chrome:9222", http = ())), bound only to the admin Worker as BROWSER_CDP (packages/cli/src/selfhost/browser-sidecar.ts).
  • Driver. The new @cloudhal/cloudflare browser-cdp.ts holds devtoolsRenderer. It reads GET /json/version, then opens the /devtools/browser/<id> socket, both with Host: localhost: Chrome refuses DevTools HTTP for any other host name, and the binding delivers the request to the sidecar whatever the URL says. From there the shared cdpSession drives it.
  • Choosing a renderer. adminRender(env) replaces the inline choice in admin-worker.ts. Browser Rendering (BROWSER) wins when present, otherwise the sidecar. CLOUDHAL_RENDER=off still turns both off. createAdminWorker>fetch gets shorter, so the size baseline holds.
  • Message size. With a sidecar configured, the admin Worker gets increase_websocket_message_size, so a DevTools message above 1 MiB (a tall screenshot) is not refused. Browser Rendering chunks its messages; a plain Chrome doesn't.

The rules from the issue

  • Reachable from the instances only.
    • Compose: the chrome profile publishes no port and runs on its own render network, shared with Cloudhal and nothing else, so the browser cannot reach SeaweedFS.
    • Helm: chrome.enabled adds a Deployment, a Service and a NetworkPolicy that admits only the release's instances on 9222. There is no Ingress.
    • Swarm: the README says to add it with no published port.
  • One browser context per render. cdpSession without closeBrowser: Target.createBrowserContext, then disposeBrowserContext on close. The shared browser is never closed. The unit test checks the exact commands, and the smoke checks that no page of the render is left in Chrome.
  • Only the existing URL rules. The browser opens nothing but the page's own signed preview link (previewDoc's token). New RenderDeps.origin (CLOUDHAL_RENDER_ORIGIN, default http://cloudhal:<site port>) opens the same signed link at the private origin the sidecar reaches the site at, with previewUrlAt in core-ts. This also covers a self-host site with no domain, which until now had no preview link at all. The origin is one value for all instances, because the config must be identical (RFC-0047 §2, invariant 2).

CI

  • New step: scripts/render-smoke.ts. It refuses to run outside GitHub Actions.
    1. SeaweedFS and an instance start on one network, and Chrome (the compose file's pinned image) on a separate network with no port, connected to the instance as cloudhal.
    2. The owner publishes and makes an agent key.
    3. initialize and tools/call render_page go over /mcp, and a JPEG of more than 2 KB must come back.
    4. /json/list must show no page left over.
  • Existing checks extended.
    • The compose check covers --profile chrome.
    • The chart lint renders chrome.enabled.
    • With the defaults the chart and compose render as before, so the kustomize base is unchanged.

Tests and docs

  • Unit tests.
    • packages/cloudflare/test/browser-cdp.test.ts: the requests, the context lifecycle with no Browser.close, the socket-path check, and which renderer is chosen.
    • packages/mcp-server/test/render-tools.test.ts: the render origin, with and without a domain.
    • packages/cli/test/selfhost-browser.test.ts: the settings and config.
  • Docs. A new "Screenshots for agents" section in the self-host guide, plus Environment rows; the Limits section no longer lists render_page. RFC-0047 §6, deploy/docker/README.md, deploy/helm/cloudhal/README.md, deploy/swarm/README.md, AGENTS.md and .env.example are updated too.
  • Changeset. cli, cloudflare and mcp-server minor; core-ts patch.

Checked locally

  • biome check --diagnostic-level=error, pnpm size:check and actionlint.
  • tsc of cli, cloudflare, mcp-server, core-ts and the smoke script.
  • vitest on selfhost-browser/selfhost, browser-cdp, render-tools and preview-token: all pass.

No Docker, compose or helm runs happened locally. This PR's Build edge run is the first time workerd talks to Chrome: the external service binding, the WebSocket upgrade and the compat flag.

Closes #292

🤖 Generated with Claude Code

CLOUDHAL_BROWSER_URL (Chrome's DevTools port, http://chrome:9222) makes
the generated workerd config bind an ExternalServer to the admin Worker as
BROWSER_CDP. @cloudhal/cloudflare's new devtoolsRenderer drives it with the
shared cdpSession: /json/version and the browser socket over Host:
localhost, one browser context per render, disposed after it; the shared
browser is never closed. adminRender picks Browser Rendering first, else
the sidecar, and CLOUDHAL_RENDER=off still turns both off.

render_page opens only the page's own signed preview link. RenderDeps.origin
(CLOUDHAL_RENDER_ORIGIN, default http://cloudhal:<site port>, the same on
every instance) opens that link at the private origin the browser reaches
the site at, so a self-host site without a domain renders too
(core-ts previewUrlAt). The admin Worker gets
increase_websocket_message_size while a sidecar is configured, for
DevTools messages above 1 MiB.

Recipes: compose's chrome profile (chromedp/headless-shell pinned, no port,
its own network shared with Cloudhal only, so it cannot reach the store),
chrome.* Helm values (Deployment, Service, NetworkPolicy admitting the
release's instances only), Swarm docs. CI: scripts/render-smoke.ts calls
render_page over /mcp with an agent key and checks a JPEG comes back and
no render page is left; the compose and chart checks cover the new parts.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Signed-off-by: Madraka <8898640+Madraka@users.noreply.github.com>
@Madraka
Madraka merged commit a36a274 into main Nov 29, 2026
12 checks passed
@Madraka
Madraka deleted the claude/selfhost-chrome-292 branch November 29, 2026 09:01
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.

Self-host: a Chrome sidecar for render_page, and the compose chrome profile

1 participant