# The browser

> Let the agent use a headless Chrome of its own to check its work, or your own signed-in browser through zelk's extension.

zelk offers the agent two browsers, and they are separate features with
separate tools and rules:

- **Its own browser** (`-browser`): a headless Chrome with an empty
  profile, for opening the app it is building, clicking through it,
  reading the console and network, taking screenshots and measuring
  performance. Its tools are named `browser_*`.
- **Your browser** (`-user-browser`): your own Chrome, Brave or Edge,
  signed in as you, reached through zelk's extension, for work on the sites
  you use. Its tools are named `user_browser_*`.

A rule for one never applies to the other, so a rule written for testing
`localhost` can't spend a real account.

zelk's own browser is available on Linux, macOS and Windows. The pinned
Windows build is for amd64. Linking your signed-in browser is available
on Linux, macOS, native Windows, and Windows browsers from WSL2.

## The agent's own browser

Install the Chrome zelk drives once:

```sh
zelk browser setup    # download zelk's pinned Chrome for Testing
zelk browser status   # what is installed, and whether your browser is paired
```

`setup` downloads the build pinned in zelk's source and installs it under
the state directory only if its size and SHA-256 match; nothing else is
downloaded or run. On Linux it names any shared libraries Chrome is missing
and the packages that provide them. `-browser-path PATH` uses a Chrome you
installed yourself instead, which zelk can't check, and `zelk browser setup
-verify` checks every installed file.

Then give a session the browser:

```sh
zelk -browser
zelk -p -browser "open http://localhost:3000 and check the sign-up form works"
```

Each session gets its own Chrome, shared with its subagents and closed
after 30 idle minutes. Its profile starts empty and is never yours.

About thirty tools come with it. The common ones are always offered
(`browser_open`, `browser_snapshot`, `browser_click`, `browser_fill`,
`browser_screenshot`, `browser_console`, `browser_network`, …); the rest
(emulation, tracing, audits, request mocking, …) are found through
`ToolSearch` when needed. Tools that only read the page run without asking
in every mode that allows reads; tools that act on it ask as any tool does.
Rules can name sites, as WebFetch rules do:

```sh
zelk -browser -allow 'browser_*(domain:localhost)'
```

**In the sandbox.** Under `-sandbox bwrap`, `seatbelt`, or `mxc` the browser runs
inside the sandbox, with the commands' network: none by default, or the
`-network-allow` list on `bwrap`. `-browser-local 3000,5173` lets it reach those
ports on this machine's loopback under `bwrap` or `seatbelt`, though its network
is off; under `mxc`, host loopback stays closed, so `-browser-local` and
`-network allowlist` are refused. Under `seatbelt` and `mxc`, Chrome runs without
its own sandbox, which can't run inside those sandboxes.

## Your own browser

The agent can work in your real browser: the sites you are signed in to,
in tabs you can watch. Set it up once:

```sh
zelk browser extension   # write zelk's extension to a directory
```

Open `chrome://extensions` (`brave://extensions`, `edge://extensions`),
turn on Developer mode, choose **Load unpacked** and pick the directory it
names. On native Windows also run `zelk browser native-messaging` to
register this zelk executable for your Windows user. Chrome, Chromium,
Edge, Brave, Vivaldi, Opera and Opera GX use the registered host.

If zelk runs in WSL2, write the extension with the WSL2 zelk. Load the
Windows path it prints and leave the Windows registration step out. Then
pair zelk with it:

```sh
zelk browser pair        # prints a one-time code to type into the extension's panel
```

zelk and the extension meet on loopback ports 27823 to 27830, one for each
zelk process running at once. If none of them can be bound, because something
else uses them or the system reserves them (Windows reserves port ranges, for
WSL2's mirrored networking and Hyper-V among others), pair on another base
port with `zelk browser pair -port 28100` and type the same number into the
extension panel's port field.

In the terminal UI, **Pair with your browser…** in the palette does the
same, and so does turning on **Your browser** in Settings › Tools. Then
start a session with it:

```sh
zelk -user-browser
```

**What the agent sees.** zelk works in one tab group, titled `zelk`. Tabs it
opens go there, in the background; you hand it a tab with the extension's
**Share**, or by dragging the tab into the group. Every other tab is
invisible to it, not even listed. It closes only tabs it opened; closing a
tab you shared gives it back.

**What asks.** A tab in the group is trusted: you shared it, approved
opening it, or a trusted tab opened it as a popup. While you are there, the
agent works in a trusted tab without asking, wherever the tab navigates.
Opening a new tab asks you, unless a rule names the site or your message
did; the prompt offers to trust the site for the session or for good.
Auto mode's reviewer never decides for you here. Taking a tab out of the
group ends its trust, and the extension's **Stop** ends the agent's work
at once, until you resume.

**What it never does.** It never types into a password, one-time-code or
card field: it hands that step to you (`user_browser_handoff`), and you
press Done in the extension's panel. It hides those fields' values, opens
only `http`, `https` and `about:blank` pages, and never sees your cookie
values. It has no file uploads, request rewriting or access to your
Downloads folder: a file it downloads is saved where zelk keeps the
browser's files, and the agent copies it where it is wanted.

**Pace.** Input goes in as a person's would: the pointer moves along a
curve, keys go in about a tenth of a second apart, with pauses between
actions. `-user-browser-pace fast` sends it at once, for your own apps
where speed matters more.

**Sessions.** One session holds your browser at a time, letting go when it
ends or after 30 idle minutes. Your browser runs as you, with your network,
outside zelk's sandbox. In a run no one watches (`zelk exec` without
`-input prompt`), it acts only where an allow rule or one of your hooks
allows it, whatever the mode,
and fails at once if your browser and its extension aren't connected
within 35 seconds.

## Settings

| Flag | Setting |
|---|---|
| `-browser` | `"browser": true` |
| `-browser-local 3000` | `"browser_local": "3000"` |
| `-user-browser` | `"user_browser": true` |
| `-user-browser-pace fast` | `"user_browser_pace": "fast"` |

Settings go in `~/.config/zelk/config.json`.

## Where to go deeper

- [The browser contract](/internals/browser/): setup, confinement, the tools
  and their permissions
- [Your browser](/internals/user-browser/) and
  [automating it](/internals/browser-automation/): trust, the hard limits,
  watches and pace
- [Cron and Watch](/cli/cron-watch/): watching a page for changes
