
# Local development with agc up

A mirror is your own masked copy of production's database, plus your app running natively on your machine against it, inside an OS sandbox. `agc up --detach` makes one per git branch and returns once the app answers: `"ok": true` with `data.url` and the actual ports in `data.ports`.

It installs dependencies, migrates and seeds a mirror that starts empty, and starts your `dev` command and worker. `agc exec -- <command>` runs one command against it.

## Ask your agent

```text
Start my mirror for this branch and tell me the local address. Then add a
"cancel booking" button and check it works there before shipping anything.
```

You'll get a `http://127.0.0.1:<port>` address to open. Sign-in on it is a test page where you can be anyone.

## Steps

```bash
agc up --detach
agc status
agc down
agc up feature-x            # a mirror with an explicit name
agc up --at 6h              # production six hours ago, as its own mirror (or an ISO 8601 time)
agc down --delete           # stop it and delete the mirror and its data
agc outbox                  # mail the mirror's app sent, captured
agc up --deps-only          # install dependencies only: no copy, no app, no credential needed
```

A successful `agc up`, trimmed:

```json
{
  "ok": true,
  "product": "agent.cloud",
  "command": "up",
  "data": {
    "mirror": "main",
    "url": "http://127.0.0.1:8080",
    "ports": { "web": 8080, "app": 9080, "database": 5432 },
    "env": "DATABASE_URL",
    "created": true,
    "migrated": ["0001_init"]
  },
  "next": [{ "cmd": "agc migrate", "why": "apply any new files in migrations/ to the mirror" }]
}
```

With an open item you own, the first `agc up` also returns `data.live` and `data.tell_person`: pass that line to the person ([The Live screen](/docs/ship/the-live-screen.md)).

## How it works

| Stage | What `agc up` does |
|---|---|
| Preflight | A uv project needs `uv`, a named `seed` must exist, required `[secrets]` need values, and the machine needs an OS sandbox (`sandbox-exec` on macOS, bubblewrap on Linux) |
| Name | The current git branch, lowercased, other characters turned into `-`, at most 41 characters; `dev` outside git, before the first commit or on a detached HEAD. An `agc/` build branch (`agc branch`) uses the mirror already running for the folder |
| Reuse | A running mirror whose app answers within 5 s is `"reused": true` at once. A changed worker, `[auth]` or env, a replaced copy, or an app that stopped answering restarts the processes, keeping the database; `data.restarted_for` says why |
| Fork | A masked copy-on-write branch of production. With `--at`, production at that moment, under its own name (`at-<yyyymmddhhmm>`), so it never replaces the mirror you work in |
| Dependencies | From your lockfiles, only when needed ([Dependencies and install scripts](/docs/build/env-mirror-and-shell-environment.md#dependencies-and-install-scripts)). A `pom.xml` app's come from Maven Central, then its jar builds offline in the sandbox (`mvn -o -DskipTests package`, up to 10 minutes); a failure is `mirror.deps_failed` |
| Migrate and seed | A mirror that starts empty (a new app nobody has used, or every mirror on a private target) gets your migrations, then your [seed](/docs/data/mirror-db-tools.md#seed-data); one with production data gets neither, so run `agc migrate` |
| Ports | Web from `[service.web] port` (default 8080), database from 5432, each moving to the next free port (a host without IPv6 works). The app listens on an inner block of five ports, normally 1000 above the web port and always below 32768, given in `PORT` |
| Start | A detached supervisor starts the web, database and egress proxies, then your app and worker inside the sandbox, and waits up to 60 s for `health` (or `/`). It runs `[service.web] dev`, else `command`, else `npm run dev`; with none of those and no `npm start`, it runs your own Dockerfile's `ENTRYPOINT` and `CMD` with its `ENV`. Keys: [manifest reference](/docs/reference/agentcloud-toml-reference.md#what-the-keys-do) |
| Local edge | The web port acts like Agent Cloud's edge: a cross-origin `POST`, `PUT`, `PATCH` or `DELETE` gets 403 `cross_origin` unless its path is in `[service.web] cross_origin`, and for `[auth] provider = "edge"` the signed-in person is `AGC_EDGE_DEV_USER` (default `dev@mirror.invalid`). WebSockets pass through |
| Reload on save | Your `dev` command reloads itself; a plain `node …` start command with no `dev` gets `--watch`. The worker restarts when it exits, after 1 s, doubling to 30 s if it keeps dying |
| Lifecycle | `agc down` stops the processes; the mirror and its data stay for `agc up` to resume, across your ships |

## Running one command with agc exec

`agc exec` runs one command in your app folder, inside the same OS sandbox and with your mirror app's environment. It returns one JSON envelope with the exit code and scrubbed output. Success is `"ok": true` with `data.exit_code: 0`.

```bash
agc exec -- npm test
agc exec -- node scripts/backfill.js --dry-run
agc exec feature-x -- uv run pytest      # a named mirror
```

Everything after `--` is passed through untouched.

| `agc exec` | |
|---|---|
| Finds | This branch's mirror (or the named one) from local state; `agc up` must have started it and `agc down` not stopped it |
| Environment | As for the app ([.env.mirror and the shell environment](/docs/build/env-mirror-and-shell-environment.md#envmirror-and-the-shell-environment)), plus the connection variables an owner allows, fetched for this run and never written down. Package managers are offline |
| Returns | `data.mirror`, `command`, `exit_code`, and `stdout` and `stderr`, each its last 8000 characters (prefixed `…` when cut) with every known credential value replaced by `[set by agent.cloud]` |
| Reaches | Only the mirror's database relay, web port and egress proxy, so a test calling a real API needs that host in `[mirror] egress_allow` |

For SQL, use `agc db query`.

## Reference

Flags: [CLI reference](/docs/reference/cli-reference.md#commands). `--at` takes ISO 8601 or an age (`90m`, `6h`). `--detach` is accepted, but `agc up` always returns once ready and keeps the mirror running.

## Errors

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

```json
{
  "ok": false,
  "product": "agent.cloud",
  "command": "up",
  "error": {
    "retryable": false,
    "code": "mirror.app_not_ready",
    "message": "The mirror's database is ready, but the app did not answer on :8080/api/health within 60s.",
    "fix": "Read the log (/Users/pat/.agentcloud/mirrors/camp-bookings/main.log); usually a crash at start-up or a different port than [service.web] port."
  },
  "next": [{ "cmd": "agc down", "why": "stop it before retrying" }]
}
```

| `code` | exit | Cause | Fix |
|---|---|---|---|
| `mirror.app_exited` | 1 | The app exited before it answered; `data.app_exit` has `exit_code` and its last lines | Fix the start command, then `agc down` and `agc up`. `data.app_exit.not_found` names a program the output says was missing; add a `[service.web] dev` command that starts without it (`data.app_exit.dev_command` suggests one) |
| `mirror.app_crashed` | 1 | The app printed a crash and stayed up (`node --watch` waiting for a change) | Read the log at `data.log`, fix the code, save |
| `mirror.app_not_ready` | 1 | No answer on `health` within 60 s; `data.not_ready` says why | Listen on the port in `PORT`; the message names it |
| `mirror.secret_missing` | 2 | A name in `[secrets] required` has no value on the mirror; `data.missing` names them | Add test values to `.env.mirror` |
| `mirror.bad_at` | 2 | `--at` unreadable, or not 1 minute to 23 hours ago | `--at 6h` or ISO 8601 |
| `mirror.isolation_unavailable` | 1 | No OS sandbox | macOS: `/usr/bin/sandbox-exec`. Linux: install bubblewrap (the installer says so too) and allow unprivileged user namespaces. Windows: run `agc up` in WSL2 with bubblewrap |
| `deps.npmrc` | 2 | A repo `.npmrc` sets more than plain install settings (`save-exact=true` and the like) | Remove that line from `.npmrc` |
| `exec.failed` | 1 | The command exited non-zero | Read `data.stdout` and `data.stderr` |

## Limits

Ready wait 60 s; `--at` 1 minute to 23 hours back; names 41 characters; `agc exec` output 8000 characters per stream.

## Agent notes

- The mirror runs your `dev` command, not production's image. Passing on the mirror doesn't prove the image starts; the rehearsal does.
- Use the ports in `data.ports`, never the manifest's: either may have moved.
- After editing `.env.mirror` or a connection's access, run `agc down` and `agc up`; `agc exec` reads both fresh each run.
- Never put production credentials in `.env.mirror` to "make it work"; use test keys.
- Package installs fail inside `agc exec`: the sandbox is offline for registries. Install outside, then `agc up`.
- Scrubbing hides values agc knows about. A secret your command builds or reads from elsewhere and prints isn't hidden.
- `agc exec` forwards no input and returns output when the command ends, so run commands in their non-interactive mode.

## Related

- [Mirror vs production](/docs/concepts/mirror-vs-production.md)
- [The mirror sandbox and egress](/docs/build/the-mirror-sandbox-and-egress.md)
- [Mirror DB tools](/docs/data/mirror-db-tools.md)
- [Connections](/docs/owners/connections.md)
- [Investigating a reported problem](/docs/ship/investigating-a-reported-problem.md)

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