# Chrome Shared Desktop

Share one headed Chromium-based browser (Chrome Stable) between a human and a coding agent inside a Sandbox0
sandbox. Both use the same tabs and login state, and can inspect applications
served on sandbox loopback ports.

The `chrome` template includes `sandbox0-browser`: a foreground command
that starts Chromium, its desktop, and a built-in web viewer. Open the viewer
through a Private Preview and attach the agent through local CDP. No additional
template, desktop script, or frontend is required.

The same runtime is also included in `coding-agent`. The
`chrome` template contains Chrome, the full desktop and Playwright CLI without
installing the coding-agent harnesses. In the dashboard, use **Open browser** on
the sandbox to start the supervised browser and open its private viewer. A startup
page shows progress until the desktop is ready. Closing that page does not stop
the browser; open it again to reconnect to the same supervised session.

In the viewer, **Connected** indicates a live desktop connection. **Reconnect**
appears when the connection drops. Use **Connect agent** to copy a prompt for
Codex, Claude Code, or another coding agent. The prompt identifies this sandbox
and explains how to attach to its existing local CDP endpoint through
authenticated Sandbox0 tooling. Configure your agent's Sandbox0 credentials
locally; the prompt does not contain an API key or a Private Preview token.
The agent and human share the existing tabs, profile, and login state.

Opening an existing Chrome sandbox from Console updates recognized built-in
viewer assets without restarting Chrome. Customized viewer files are preserved.

## Cloud Browser

[Cloud Browser](https://cloudbrowser.tv) is a cloud browser built on the Sandbox0
`chrome` template. Visit [cloudbrowser.tv](https://cloudbrowser.tv) to use the
application, or follow this guide to build your own browser workflow with Sandbox0.

## Quickstart

Use a `chrome` image containing `sandbox0-browser`. Existing sandboxes
retain their installed files when a template changes; check availability with
`sandbox0-browser --version` inside the sandbox. If the command is missing,
use an updated template for a new sandbox or explicitly upgrade the old RootFS.
See [Coding Agents](/docs/sandbox/use-cases/coding-agents#template-updates).

Install the JavaScript SDK and save the following as `browser.mjs`:

```bash
npm install sandbox0@^0.10.1
```

```javascript
import { Client } from "sandbox0";
import { setTimeout as sleep } from "node:timers/promises";

const token = process.env.SANDBOX0_API_KEY;
const baseUrl = process.env.SANDBOX0_API_URL;
if (!token || !baseUrl) {
    throw new Error("SANDBOX0_API_KEY and SANDBOX0_API_URL are required");
}
const client = new Client({
    token,
    baseUrl,
});
const sandbox = await client.sandboxes.claim("chrome", {
    ttl: 3600,
    hardTtl: 7200,
    resources: { memory: "2Gi" },
});

try {
    const session = await sandbox.createSession({
        command: ["sandbox0-browser", "--require-version", "2"],
        cwd: "/workspace",
        readiness: {
            type: "output",
            output: "Browser ready:",
            timeoutMs: 60000,
        },
        lifecycle: {
            restart: { policy: "on_failure" },
            runtimeRecovery: "restart",
        },
    }, { idempotencyKey: "shared-browser" });

    let ready = false;
    for (let attempt = 0; attempt < 75; attempt++) {
        const current = await sandbox.getSession(session.id);
        if (current.phase === "running") {
            ready = true;
            break;
        }
        if (["failed", "exited", "stopped", "suspended"].includes(current.phase)) {
            throw new Error(`Browser session ended: ${current.phase}`);
        }
        await sleep(1000);
    }
    if (!ready) throw new Error("Browser readiness timed out");

    const preview = await sandbox.createPreview({
        port: 6080,
        protocol: "http",
        path: "/",
        ttlSeconds: 900,
    });
    console.log("Sandbox:", sandbox.id);
    console.log("Browser session:", session.id);
    console.log("Preview grant:", preview.id);
    console.log("Open this one-time link in your browser:", preview.url);
} catch (error) {
    await client.sandboxes.delete(sandbox.id);
    throw error;
}
```

Set `SANDBOX0_API_KEY` and `SANDBOX0_API_URL` for your deployment, then run
`node browser.mjs`. Open the printed link in your browser to see and control
Chromium. Keep this link private and do not fetch it from your backend first:
it contains a one-time credential that the browser exchanges for a preview
cookie. The SDK/API key stays in your backend or local script.

The supervised session keeps running after this script exits. A Private Preview
expires independently; renew it before expiration or create a new grant to open
another viewer. See [Private Previews](/docs/sandbox/previews) for renewal,
revocation, and runtime generation behavior.

To stop the browser, call
`sandbox.setSessionDesiredState(sessionId, "stopped")`. To remove the test
sandbox and its RootFS, call `client.sandboxes.delete(sandboxId)`.

## Use An Existing Sandbox

Run the helper in a supervised session rather than an interactive terminal:

```bash
s0 sandbox session create "$SANDBOX_ID" \
    --restart on_failure --runtime-recovery restart \
    --readiness output --ready-output 'Browser ready:' \
    --ready-timeout-ms 60000 -- sandbox0-browser --require-version 2
```

Wait until the session is `running`, then create a Private Preview for port
`6080` as above or with the HTTP API:

```bash
curl -X POST "$SANDBOX0_API_URL/api/v1/sandboxes/$SANDBOX_ID/previews" \
    -H "Authorization: Bearer $SANDBOX0_API_KEY" \
    -H "Content-Type: application/json" \
    -d '{"port":6080,"protocol":"http","path":"/","ttl_seconds":900}'
```

Open `data.url` in your browser. Creating a preview does not start the browser
process. Run only one shared browser per sandbox; starting a second helper fails
instead of taking over an active profile.

## Attach The Agent

The CDP endpoint stays on sandbox loopback. An agent outside the sandbox can
execute Playwright through Sandbox0's execution API; do not publish bare CDP.

With the browser ready, run these commands inside the same sandbox:

```bash
playwright-cli attach --cdp=http://127.0.0.1:9222
playwright-cli tab-list
playwright-cli snapshot
```

Read the installed CLI's version-matched Agent Skill before using automation
commands. `attach` reuses the existing browser and tabs. A separately launched
browser or isolated context does not share the same interaction. See the
[upstream Playwright CLI guide](https://github.com/microsoft/playwright-cli).

The human can sign in through the viewer and let the agent continue in the same
browser. Coordinate before navigating, typing, or closing tabs: sharing a
browser does not serialize human and agent input. When the agent finishes, use
`playwright-cli detach` to leave Chromium running. Closing the viewer also leaves
the browser running.

## Memory Saver and idle page sessions

Updated browser helpers configure Memory Saver's maximum mode before starting Chrome, including existing profiles when their helper is next started. The update preserves unrelated Local State fields and never edits a running profile. Use `--memory-saver preserve` to retain the profile's own choices, or `--memory-saver off` to explicitly disable it; `SANDBOX0_BROWSER_MEMORY_SAVER` accepts the same values. Older installed helpers require an explicit runtime update; changing a template does not change an existing RootFS.

Maximum mode is an idle timer, not a guarantee of immediate recovery under host memory pressure. Chrome 156 on Linux was verified to automatically discard eligible, detached idle pages after about two hours. Attached debugger sessions are protected; sites with activity or unsaved state may also remain ineligible. A discarded page reloads on return and does not retain its JavaScript heap. Release page-level CDP sessions when an agent has finished with them, and rediscover/reattach when needed rather than keeping every page attached indefinitely. Do not disconnect another agent's active session based only on a quiet connection.

Host `sandbox.host.*` runtime metrics expose the resource cgroup's actual memory, swap and PSI when available. Guest `/proc/meminfo` may report no swap despite host swapping. Use fresh, generation-matched host samples for admission or recovery decisions; unsupported observations are missing, not zero. Chrome's Memory Saver does not itself connect these host metrics to an urgent-discard policy on Linux.

## Defaults And Configuration

Runtime version 2 includes wallpaper, a bottom taskbar and browser launcher.
Task buttons and the launcher restore minimized windows without duplicating
them. Closing the final browser window keeps the desktop and viewer connected;
the launcher reopens the same profile, retaining cookies. CDP is restarted
without opening a window, so automation can reconnect after Chrome exits.
Native maximize/restore and F11 fullscreen are available. Normal windows are
fitted to the current work area after restoring or shrinking the desktop, and
the taskbar reserves its space when windows are maximized. Small viewers scale
a desktop of at least 640×480 while preserving their aspect ratio.

| Setting | Default |
|---------|---------|
| Viewer HTTP and WebSocket | `127.0.0.1:6080`, page `/`, WebSocket `/vnc` |
| Readiness endpoint | `GET /healthz`, available after desktop and CDP readiness |
| Agent CDP | `http://127.0.0.1:9222` |
| Desktop VNC | `127.0.0.1:5900` |
| Persistent browser profile | `/workspace/.browser/profile` |
| Persistent downloads | `/workspace/browser-downloads` |
| Browser user | `sandbox-browser`, with Chromium's process sandbox enabled |
| Initial desktop size | `1440x900` |
| Launcher control | `127.0.0.1:9224`, local `POST /open`, browser Origins rejected |

Use `sandbox0-browser --help` for command options. For example:

```bash
sandbox0-browser --profile /workspace/my-browser/profile \
    --downloads /workspace/my-browser/downloads --size 1920x1080
```

Profile and download paths must be separate canonical directories under
`/workspace`, with no symbolic-link ancestors. The helper prepares directory
permissions, serializes startup, and recovers stale Chromium singleton links
only after checking that the profile has no active browser process. Stopping
the helper stops its desktop, window manager, Chromium, and viewer together.
Launch the helper as sandbox root; it starts Chrome and the window manager
as the dedicated browser user.

The viewer is bundled with the image; browser and desktop dependencies are
installed at build time. Claiming a sandbox does not automatically start them.
The helper has no independent authentication: keep its default loopback bind
and use Private Preview, or protect every published route with service auth.
CDP and VNC remain on loopback and are not exposed by the viewer.

## Preview Local Web Applications

Open `http://127.0.0.1:3000` in the shared Chromium to inspect a development
server in the same sandbox. Here, `localhost` refers to the sandbox. Your device
connects to the viewer while the remote browser connects to the web app, so you
do not need to publish each development server.

For a direct link rendered in the human's own browser, use a
[Private Preview](/docs/sandbox/previews) of the application's port.

## Integrate Into Your Application

Applications can use the same helper with an existing noVNC frontend and
an authenticated backend proxy. Register a
command-backed [Sandbox Service](/docs/sandbox/services) that exposes only the
VNC bridge to your backend:

```yaml
services:
    - id: shared-browser
      port: 6080
      runtime:
          type: cmd
          command: [sandbox0-browser, --require-version, "2", --host, 0.0.0.0]
          cwd: /workspace
      ingress:
          public: true
          routes:
              - id: viewer
                path_prefix: /vnc
                methods: [GET]
                auth:
                    mode: header
                    header_name: X-Browser-Proxy
                    header_value_sha256: <sha256-of-backend-route-token>
                resume: true
```

The helper uses `SANDBOX0_SERVICE_PORT` when a service injects it. It opens the
viewer listener only after the desktop and CDP are ready, so the default TCP
service readiness check covers browser startup. Include all services you want
to retain when updating the service list: `PUT /services` replaces the list.

Your backend authorizes access to the sandbox and relays the frontend's
WebSocket to the service's `public_url` plus `/vnc`, using `wss` and the configured
auth header. Browser clients cannot set that header directly. Keep the route
token and deployment API key in the backend. Enable sandbox `auto_resume` and
route `resume` when viewing should wake a paused sandbox.

When integrating this command into an existing application, retain its browser
profile path and upgrade existing RootFS generations to runtime version 2.
Applications must not assume a template promotion changes files already
installed in an existing sandbox.

### Upgrade An Existing RootFS

The candidate build workflow produces `browser-runtime.tar.gz` and records
its SHA-256 in `coding-agent-release.json`. These assets belong to private
candidate prereleases; building them does not qualify them for production or
make them publicly downloadable. Obtain an approved exact-image archive from
your platform operator. The archive is checked to be identical across
architectures. Applications can use it to upgrade
only the browser runtime on an existing RootFS, preserving workspace, profile,
and Agent processes.

Stop only the supervised Browser session/service, extract the verified archive
into a separate staging directory, and run
`bash /path/to/staged-runtime/upgrade-runtime.sh /path/to/staged-runtime` as
sandbox root. The installer refuses occupied Browser ports, installs missing
desktop packages, creates the template's browser user if absent, retains the
previous runtime for recovery, and installs the shared launcher. The archive
includes its WebSocket library, so it does not depend on that library being
installed by a retained coding-agent image. Restart the Browser service and verify
`sandbox0-browser --version`, the viewer, and your Agent's process continuity.
The helper uses the existing profile when passed with `--profile`.

Archived snapshots retain their original binaries. Rerun this upgrade after
restoring an old snapshot. Do not replace RootFS or use disk-only pause/resume
to upgrade a live Environment. If network policy blocks package repositories,
provision the required packages separately or arrange an explicit policy change.

## Persistence And Recovery

| State | Recovery behavior |
|-------|-------------------|
| Committed profile files, cookies, and local storage | Restored with RootFS; websites may still expire login sessions |
| Downloads | Restored with RootFS |
| Browser and desktop processes | Restarted after filesystem-only recovery by the session/service; supported memory checkpoint/restore can retain execution state |
| Viewer and CDP connections | Reconnect after resume or transport failure |
| Live tabs and in-memory page state | Not guaranteed by RootFS recovery or Chromium session restore |
| Private Preview grants | Recreate after runtime replacement; renew before expiration |

See [Pause And Resume](/docs/sandbox/pause-resume) for memory checkpoint support
and compatibility. Profile persistence does not guarantee continuation of an
in-flight browser operation. Reattach the agent when its CDP connection is lost.

RootFS snapshots and forks include profile files, which can contain authenticated
sessions. Anyone with workspace access can access those files; grant access to
copies accordingly.

## Next Steps

<CardGroup>
  <Card title="Private Previews" href="/docs/sandbox/previews" cta="Continue">
    Renew or revoke access to the built-in viewer.
  </Card>
  <Card title="Sandbox Services" href="/docs/sandbox/services" cta="Continue">
    Integrate protected viewer ingress and startup on first access.
  </Card>
  <Card title="Coding Agents" href="/docs/sandbox/use-cases/coding-agents" cta="Continue">
    Use the same shared desktop beside an installed coding agent.
  </Card>
</CardGroup>

## Desktop and remote CDP with one runtime

Configure both authenticated services together when an agent connects from outside
this sandbox. The CDP service references the desktop command owner, so either
entry starts the same supervised session and waits for its own endpoint to become
ready. Concurrent requests reuse the owner's session. The optional CDP listener
is disabled unless `--cdp-proxy-port` is supplied; local agents can still use 9222.

```yaml
services:
  - id: desktop
    port: 6080
    runtime:
      type: cmd
      command: [sandbox0-browser, --host, 0.0.0.0, --port, "6080", --cdp-proxy-port, "9223"]
      cwd: /workspace
    health_check: {path: /healthz}
    ingress:
      public: true
      routes:
        - id: viewer
          path_prefix: /
          auth: {mode: bearer, bearer_token_sha256: "<sha256-of-route-token>"}
          timeout_seconds: 60
          resume: true
  - id: cdp
    port: 9223
    runtime: {type: service, service_id: desktop}
    health_check: {path: /json/version}
    ingress:
      public: true
      routes:
        - id: cdp
          path_prefix: /
          auth: {mode: bearer, bearer_token_sha256: "<sha256-of-route-token>"}
          timeout_seconds: 60
          resume: true
```

Use the route bearer token for both CDP discovery and WebSocket attachment. The
CDP response rewrites its WebSocket address to the authenticated service origin.
Neither the public CDP proxy nor its upstream requests receive your Sandbox0 API
key. The gateway owns route authentication; the helper has no independent auth.
Enable sandbox `auto_resume` as well if either entry should resume paused compute.

A runtime reference can target only a different `cmd` service in the same service
list. References cannot override its command/environment or reference another
reference. Updating services must retain the referenced owner. Session lifetime
is independent of HTTP requests: closing a page or disconnecting an agent does
not stop the shared browser. Existing RootFS generations require an explicit
browser-runtime upgrade before using the new CDP flag.
