
# .env.mirror, the shell environment and dependencies

Your mirror gets test keys from `.env.mirror` at the app root, never from production or your shell. `agc up` installs your lockfiles' dependencies outside the mirror's sandbox, then runs install scripts inside it, offline. Success: after `agc down` and `agc up`, the app sees the variable and starts.

## Ask your agent

```text
The mirror needs my Stripe test key. Tell me exactly what to put in .env.mirror
(not the value, I'll type it), and move anything else the app read from my shell
in there.
```

The agent should never ask you to paste a key into chat; you edit the file yourself.

## Steps

```bash
# .env.mirror, next to agentcloud.toml
STRIPE_SECRET_KEY=sk_test_…
LOG_LEVEL=debug             # a plain shape: prints as written
```

```bash
npm install date-fns     # add packages outside the mirror (or: uv add httpx)
agc down
agc up --detach
agc up --deps-only       # install and run install scripts only: no copy, no app
```

Restart after editing `.env.mirror`; each `agc exec` reads it fresh.

## .env.mirror and the shell environment

Production secrets never reach a mirror, and nor do your shell's keys. Test keys go in `.env.mirror` at the app root, one `KEY=value` per line. The app, the worker, `agc exec`, install scripts and the seed command get it. Production, rehearsals, snapshots and the app's GitHub repo never do.

| Rule | |
|---|---|
| Other files | agc loads no other `.env`; a dev command loading one itself (dotenv, `--env-file`) can, unhidden |
| From your shell | Only the basics in the [runtime contract](/docs/reference/app-runtime-contract.md#on-a-mirror-only), which gives the order; agc's own variables win, so `PORT` in `.env.mirror` does nothing. `NODE_OPTIONS`, `AGC_*` and `AGENTCLOUD_*` are held back too |
| Held-back warning | `agc up`'s `warnings` names up to 40 held-back variables, never values |
| Missing secret | A `[secrets] required` name it lacks: `mirror.secret_missing` (exit 2) before anything starts, naming the `NAME=<test value>` line |
| Lines | `KEY=value`, key `[A-Za-z_][A-Za-z0-9_]*`. Blank lines, `#` comments and a leading `export ` are ignored; an unparsable line is skipped silently |
| Values | In matching quotes, taken as is; otherwise a trailing ` # comment` is stripped. No expansion, no multi-line values |
| Repo | `agc init` gitignores it unless a `.env.mirror`, `/.env.mirror` or `.env*` line exists; snapshots leave out every `.env*` file |
| Shown as written | In the log and `agc exec` output: a line ending `# plain`, or a plain shape (`true`, `8080`, `staging`, `debug`) under a name without `SECRET`, `TOKEN`, `KEY`, `PASS`, `AUTH` or similar |
| Hidden | `# secret` or in `[secrets] required`: `[set by agent.cloud]` wherever it appears from 3 characters. Any other: from 8 (3 under a credential's name), its line `NAME=[set by agent.cloud]`. While agentcloud.toml doesn't parse: all |

## Dependencies and install scripts

A fresh clone needs nothing preinstalled. Before the app starts, `agc up` installs what your lockfiles name outside the sandbox, with lifecycle scripts off and none of your credentials in the environment. Install scripts then run inside, offline. With nothing new, it skips the step.

| Lockfile | When | Command, outside |
|---|---|---|
| `package-lock.json` | newer than `node_modules/.package-lock.json` | `npm ci --ignore-scripts --no-audit --no-fund --git=<trusted git>` |
| `package.json` only | no `node_modules` | `npm install --ignore-scripts …` |
| `pnpm-lock.yaml` | newer than `node_modules/.modules.yaml` | `pnpm install --frozen-lockfile --ignore-scripts …` |
| `yarn.lock` (yarn 4 or later) | newer than `.yarn/install-state.gz` | `yarn install --immutable --mode=skip-build` |
| `uv.lock` + `pyproject.toml` | always | `uv sync --frozen --no-install-project --no-build --inexact --no-config --python <resolved>` |
| packaged Python project | then | `uv pip install --only-binary :all:` of its backend's requirements |

| Outside | |
|---|---|
| Tools | `npm`, `uv`, `git` from `PATH` outside the app's folder and repository, never a committed `node_modules/.bin/npm`. A project `.npmrc`'s `git=` or shell, a `.python-version` path and `uv.toml` are ignored |
| Environment | Only `PATH`, `HOME`, `USER`, `LOGNAME`, `SHELL`, `TERM`, `TMPDIR`, `LANG`, `LC_*`, `npm_config_*`, `pnpm_config_*`, `NPM_*`, `NODE_AUTH_TOKEN`, `NODE_EXTRA_CA_CERTS`, `UV_*`, `PIP_*`, `SSL_CERT_*`, proxy variables: registry tokens work, cloud keys don't |
| Python | [Wheels only](/docs/build/python-apps.md): a missing wheel fails here, saying so. No interpreter from the app folder; a `.venv` from another is rebuilt |
| Failure | `mirror.deps_failed`, last lines in `data.output_tail`; an out-of-date lockfile needs `npm install` or `uv lock` |
| pnpm | A root `pnpm-lock.yaml` makes a pnpm app; with `package-lock.json` too, `packageManager` must choose (`deps.two_lockfiles`). agc's own pnpm, picked as in [How your image is built](/docs/build/how-your-image-is-built.md), integrity-checked; the repo's pnpmfile is never run |
| yarn | A root `yarn.lock`, alone or named by `packageManager`, which must pin an exact yarn 4 or later. agc fetches that yarn and checks its hash; `yarnPath` is ignored. `.yarnrc.yml` holds only plain install settings (`nodeLinker`, `nmMode` and the others `deps.yarn_config` lists); dependencies are registry ranges, `workspace:` or `npm:` aliases |

| Inside, offline, in order | |
|---|---|
| 1. Dependencies | `npm rebuild <packages>` for each whose lockfile entry has install scripts (`pnpm rebuild --pending`; yarn runs `yarn install --immutable` again, offline); native modules build against the `node` on `PATH` (`npm_config_nodedir`), so node-gyp doesn't download |
| 2. Yours | `npm run --ignore-scripts` for `preinstall`, `install`, `postinstall`, `prepare` |
| 3. Packaged Python | `uv sync --frozen --offline --no-build-isolation --inexact` |
| 4. Seed | The seed command |
| Settings | `npm_config_offline=true`, `UV_OFFLINE=1`, `UV_NO_SYNC=1`, `PIP_NO_INDEX=1`: a package missing from the lockfile fails, not hangs |
| Until they pass | The app doesn't start; they retry next start |
| npm 12 `allowScripts` | Blocked packages are skipped as `npm ci` would and listed in `data.install_scripts_blocked` (`packages`, `note`), also on `mirror.app_crashed` / `mirror.app_not_ready`. Approving (`npm install-scripts approve <package>`, then `agc up --detach`) edits `package.json`, is the person's call, and rebuilds them |

## Errors

Every code, its exit and fix: [Error code catalog](/docs/reference/error-code-catalog.md). Plain-env codes (`mirror.env_secret`, `mirror.env_reserved`, `mirror.bad_env`): [agentcloud.toml reference](/docs/reference/agentcloud-toml-reference.md#errors).

| code | exit | cause | fix |
|---|---|---|---|
| `deps.npmrc` | 2 | A repo `.npmrc` sets more than plain install settings (`save-exact`, `node-linker` and similar), such as a registry or credential | Remove the line it names; agc installs from the registry your machine is configured for |
| `deps.pnpm_lockfile_unknown` | 2 | No pnpm for the lockfile version (known: `5.4`, `6.0`, `6.1`, `9.0`) and none pinned | Regenerate the lock, or pin pnpm in `packageManager` |
| `deps.pnpm_setting` | 2 | `pnpm-workspace.yaml` sets `python`, `cargo`, `include` or `cacheDir`, picks a registry, reads the environment (`${`), lists packages outside the app, or isn't plain YAML | Remove the setting it names |
| `deps.yarn_classic`, `deps.yarn_unsupported` | 2 | yarn 1, or yarn 2 or 3 | `yarn set version stable`, `yarn install`, commit |
| `deps.yarn_unpinned` | 2 | No exact yarn in `packageManager` | Add `"packageManager": "yarn@<version>"` |
| `deps.yarn_config` | 2 | A `.yarnrc.yml` line that isn't an allowed setting (plugins, `yarnPath`, a registry, quotes) | Keep only the lines the fix lists |
| `deps.yarn_dependency` | 2 | A git, URL, `file:`, `link:`, `patch:` or `exec:` dependency, or an unusual `workspaces` pattern | Depend on a published version, `yarn install`, commit |

```json
{
  "ok": false,
  "product": "agent.cloud",
  "command": "up",
  "data": { "missing": ["STRIPE_SECRET_KEY"], "env_file": "/Users/you/camp-bookings/.env.mirror" },
  "error": {
    "retryable": false,
    "code": "mirror.secret_missing",
    "message": "agentcloud.toml's [secrets] required lists STRIPE_SECRET_KEY, and this mirror's app wouldn't get it: production's secrets never reach a mirror, and it isn’t in .env.mirror.",
    "fix": "Add a line to .env.mirror in the app folder (gitignored, never shipped) with a test value, never production's: STRIPE_SECRET_KEY=<test value>. Then run agc up again.",
    "docs": "agc help secrets"
  },
  "next": [{ "cmd": "agc up --detach", "why": "start the mirror once .env.mirror has it" }]
}
```

## Limits

| What | Value |
|---|---|
| Held-back names in `warnings` | 40, then a count |
| `output_tail` | 15 lines |
| Install scripts, seed | 10 minutes each |

## Agent notes

- A variable your shell used to export now belongs in `.env.mirror`; check `agc up`'s `warnings`. A missing one may be a key with spaces.
- Never print, commit or ask in chat for `.env.mirror` values. Use test keys, never production's.
- Never `agc exec -- npm install`; install outside, then `agc up`. Install scripts run offline, so a `postinstall` that downloads fails with `mirror.deps_failed`.
- Every mirror of the app reads the same `.env.mirror`; `agc secret set` sets production's values, not a mirror's.
- Production's image runs its own install, with scripts ([How your image is built](/docs/build/how-your-image-is-built.md)).

## Related

- [The mirror sandbox and egress](/docs/build/the-mirror-sandbox-and-egress.md)
- [Local development with agc up](/docs/build/local-development-with-agc-up.md)
- [Secrets](/docs/build/secrets.md)
- [Python apps](/docs/build/python-apps.md)
- [How your image is built](/docs/build/how-your-image-is-built.md)
- [Runtime contract](/docs/reference/app-runtime-contract.md)

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