
# The mirror sandbox and egress

On a laptop or Devbox, your mirror's app, its worker and every `agc exec` command can leave the OS sandbox only through agc's proxy, to Agent Cloud and the hosts you grant. Grant one with `[mirror] egress_allow = ["api.stripe.com:443"]` in `agentcloud.toml`.

Success: `agc up` shows `"enforced": true` and your grant under `data.egress.allowed`. Calls to that host work; the proxy refuses others with a 403, which Node's `fetch` reports as `cause.code` `ECONNREFUSED`. The mirror's data is masked but its code is yours, so without the boundary it could call real Stripe with whatever key is lying around.

## Ask your agent

```text
The mirror can't reach the Stripe test API. Allow just api.stripe.com on the
mirror with test keys from my .env.mirror, and show me what was refused. Tell
me what to put in .env.mirror (not the value, I'll type it).
```

The grant shows in the repo's history; you type keys yourself, never in chat.

## Steps

```bash
# agentcloud.toml:  [mirror] egress_allow = ["api.stripe.com:443", "hooks.example.test:443"]
agc down
agc up --detach          # prints the granted list
agc status               # per mirror: egress.allowed, enforced, mechanism, last 10 refusals
```

A new grant needs no restart; `agc down` and `agc up` here pick up test keys you added to `.env.mirror`.

## How it works

| Piece | Behavior |
|---|---|
| macOS | `/usr/bin/sandbox-exec`, profile `(allow default)`, `(deny network-outbound)`, then `allow` for agc's sockets and the mirror's `localhost` ports: egress proxy, database relay, web port, five dev ports. The DNS socket `/var/run/mDNSResponder` is denied |
| Linux, Devbox | `bwrap --unshare-net --unshare-ipc --unshare-uts --die-with-parent` (`--unshare-pid` where a fresh `/proc` mounts): only loopback, a forwarder to agc's Unix sockets; `/usr`, `/bin`, `/sbin`, `/lib`, `/lib64`, `/etc` and toolchain prefixes read-only; the app folder, agc, an empty per-mirror `HOME`, fresh `/tmp`, `/run`, `/var/run`. With a masked `/proc` (Devboxes) it shares the process table: `bwrap-shared-pid` |
| No mechanism | No fallback, `unshare` included: `agc up` and `agc exec` refuse with `mirror.isolation_unavailable` |
| Egress proxy | On `127.0.0.1`, takes `CONNECT` and absolute-URL HTTP to the mirror's Agent Cloud endpoints (test sign-in, captured email), granted connections and granted `host:port`; else `not granted`. Resolves names itself, refuses any address in a blocked range (a connection may be private, never loopback or metadata), dials the checked address against DNS rebinding, follows no redirects |
| Refused in Node | A refused or cancelled request fails `fetch` like any network failure: a `TypeError` whose `cause.code` is `ECONNREFUSED` (refused: not granted, a private or metadata address, a retired analytics branch) or `ECONNRESET` (the destination didn't answer), with a message naming the host and port. agc puts its `--require` first in the mirror's `NODE_OPTIONS`, ahead of `.env.mirror`'s, so the app, worker, seeds and `agc exec` all get it. Your own abort or timeout stays as Node gives it |
| Proxy variables | Listed in the [runtime contract](/docs/reference/app-runtime-contract.md#environment-variables). A client that ignores them, like some gRPC and database drivers, reaches nothing |
| Maven apps | A `pom.xml` app's dependencies come from Maven Central only, fetched outside the sandbox by agc's own Maven; its build then runs inside the sandbox, offline, with no grant ([Local development](/docs/build/local-development-with-agc-up.md)) |
| Database | A loopback relay ([Your app's Postgres](/docs/data/your-apps-postgres.md#how-it-works)) |
| Grants | `host:port` ([key rules](/docs/reference/agentcloud-toml-reference.md#what-the-keys-do)); `*.example.com:443` never covers `example.com`. Reread per HTTP request and new tunnel; removing one doesn't close an open tunnel. An entry naming `localhost`, a loopback or private address, or a `.local` or `.internal` name is ignored, and `agc up` and `agc check` warn: grant the service's public host, use a [connection](/docs/owners/connections.md) for a database in your network, or a stub |
| Blocked IPv4 | `0.0.0.0/8`, `10.0.0.0/8`, `100.64.0.0/10`, `127.0.0.0/8`, `169.254.0.0/16` (metadata `169.254.169.254`), `172.16.0.0/12`, `192.0.0.0/24`, `192.168.0.0/16`, `198.18.0.0/15`, `224.0.0.0/4`, `240.0.0.0/4` |
| Blocked IPv6 | `::`, `::1`, `fc00::/7` (metadata `fd00:ec2::254`), `fe80::/10`, `ff00::/8`, `64:ff9b::/96`, `2001:db8::/32`; IPv4-mapped as IPv4 |

Test keys and your shell's variables: [.env.mirror and the shell environment](/docs/build/env-mirror-and-shell-environment.md#envmirror-and-the-shell-environment).

How `agc up` installs packages: [Dependencies and install scripts](/docs/build/env-mirror-and-shell-environment.md#dependencies-and-install-scripts).

## Reference

| `data.egress` (`agc up`), `mirrors[].local.egress` (`agc status`) | |
|---|---|
| `allowed` | The grants as the proxy applies them |
| `enforced` | `true` under `sandbox-exec`, `bwrap` or `bwrap-shared-pid` |
| `mechanism` | One of those, or the test opt-out |
| `proxy` | The proxy's address (`agc up` only) |
| `refused` | Last 10: `at`, `host`, `port`, `why` (`not granted`, `could not resolve`, `private or metadata address`, `its analytics branch was retired`); `agc status` only |

## Errors

Every code, its exit and fix: [Error code catalog](/docs/reference/error-code-catalog.md).

| code | exit | cause | fix |
|---|---|---|---|
| proxy `403` | n/a | Not granted: `…api.example.com:443 is not allowed from this mirror (not granted)` | Grant it if it should be reachable |
| `ECONNREFUSED` (`fetch` `cause.code`) | n/a | The proxy refused the host; the message names it and points at `agc status` | Check the refusal's `why` in `agc status`, then grant it or use a test endpoint |

```json
{
  "ok": false,
  "product": "agent.cloud",
  "command": "up",
  "error": {
    "retryable": false,
    "code": "mirror.isolation_unavailable",
    "message": "This machine can’t run a mirror inside an operating-system sandbox, and agc never starts one without it: its app could reach real third-party services.",
    "fix": "Install bubblewrap (sudo apt install bubblewrap, or sudo dnf install bubblewrap) and allow unprivileged user namespaces (on Ubuntu 24.04: sudo sysctl kernel.apparmor_restrict_unprivileged_userns=0), then run agc up again."
  },
  "next": [{ "cmd": "agc ship", "why": "agent.cloud rehearses the release on a branch of production, without a mirror" }]
}
```

The fix fits the machine: a Mac without `/usr/bin/sandbox-exec` is told so; on Linux, agc running as root or holding capabilities needs `setpriv` (`sudo apt install util-linux`); on Windows, run `agc up` under WSL2 with bubblewrap installed there. Devboxes are staging only, so the fix doesn't offer one.

## Limits

| What | Value |
|---|---|
| Refusals kept | Last 100 per mirror, in `~/.agentcloud/mirrors/<app>/<mirror>.egress.jsonl` (trimmed past 200) |
| Dev ports inside | 5, from `PORT` |

## Agent notes

- An outbound call fails: check `agc status` for a refusal before changing code.
- Grant test endpoints only; a production provider plus a live key defeats the boundary.
- [Connections](/docs/owners/connections.md) reach a mirror through variables an owner allows, not `egress_allow`.

## Related

- [.env.mirror, the shell environment and dependencies](/docs/build/env-mirror-and-shell-environment.md)
- [Local development with agc up](/docs/build/local-development-with-agc-up.md)
- [Production data protection](/docs/security/production-data-protection.md)
- [Connections](/docs/owners/connections.md)

<!-- feedback:start -->
---
Wrong, outdated or unclear? Run `agc feedback docs "build/the-mirror-sandbox-and-egress: <what is wrong>" --body <file>` with the sentence quoted in the file. No code or app data is sent.
<!-- feedback:end -->
