Skip to content
mentoor

Topic 2 of 5 · 5 min read

Preparing a cloud agent's sandbox

Goal: Write an environment setup that lets a cloud agent install, build and run the tests without network surprises.

A cloud agent can only prove its work if its sandbox can install your dependencies, build your project and run your tests, so prepare that environment once and write it down as a script. An agent that cannot run the tests will still open a pull request. It will just be unproven.

Check yourself · 1 of 3

Why should the sandbox setup script start with set -e?

Choose one answer

Why it matters

The sandbox is a clean machine in the cloud. It does not have your laptop's global tools, your .env file, a running database or your VPN. If setup fails, many agents carry on anyway, read the code and guess. The pull request looks fine and was never run.

What to prepare

  1. A setup script. The commands that take a fresh checkout to a working build: install the language runtime and dependencies, build step, code generation. Codex cloud and Claude Code on the web both support a setup script that runs when the environment starts. Copilot's cloud agent uses a setup workflow in the repository.
  2. Pinned versions. Pin the runtime and lockfile so the sandbox matches CI. "Works on the latest Node" is not a version.
  3. Test services. If tests need Postgres or Redis, start them in the script or use a service container, with fake data and a local-only password. Never point a sandbox at a real database.
  4. Network rules. Setup usually has internet to fetch packages. During the agent's run, access is often restricted to an allow-list. Add the package registries you need, and nothing broader. A smaller allow-list also limits where the agent can send data.
  5. Secrets. Give the sandbox only test credentials. If a task truly needs a key, scope it to read-only access for that one service.
  6. A smoke command. One line the agent runs first, such as npm run build && npm test, so a broken environment shows up in the first minute.

Before and after

Before, the agent has an empty environment and the tests fail with "command not found: pnpm":

# no setup script

After:

#!/usr/bin/env bash
set -euo pipefail
corepack enable
pnpm install --frozen-lockfile
docker compose up -d postgres   # local test database, fake data
pnpm db:migrate
pnpm build

set -e makes the script stop at the first failure, so a half-built environment is reported instead of ignored.

What goes wrong

  • Silent setup failure. The script errors halfway and the agent works on a broken environment. Fail loudly.
  • Setup works, run is blocked. Packages install fine, then a test downloads a file and the network rule blocks it. Run your tests once under the same rules.
  • Real credentials in the sandbox. A prompt-injected instruction could use them. Use test keys.
  • Drift from CI. Different versions in the sandbox give different results from the pull request checks.

Examples

A setup that fails quietly

npm install
npm run build || true

The || true hides a failed build. The agent starts work, sees odd errors, and edits code to fix problems that are really in the environment. Remove || true and add set -e.

The blocked download

A test downloads a fixture from a public bucket. It passes on a laptop and in CI, but in the sandbox the agent's network allow-list blocks the host. The agent decides the test is flaky and deletes it. The fix is to vendor the fixture into the repository, not to widen the network rules.

Key points

  • Write the environment down as a setup script so every run starts from the same state.
  • Pin runtime and dependency versions to match CI.
  • Start test services locally with fake data; never connect a sandbox to a real database.
  • Keep the run-time network allow-list as small as the tests allow, and test under it.
  • Make the script fail loudly, and give the agent a smoke command to run first.

Common mistakes

  • Relying on tools installed on your own machine that the sandbox does not have.
  • Letting setup errors pass so the agent works in a broken environment.
  • Opening network access wide to make one test pass instead of fixing the test.
  • Putting production credentials into the sandbox for convenience.

Check yourself · 2 of 3

The tests need a database. What is the safest setup for a cloud agent's sandbox?

Choose one answer

Check yourself · 3 of 3

Packages install during setup, but a test fails at run time with a blocked network call. What should you check first?

Choose one answer

Practice items, not counted towards your mastery.

Start here