# Plan First: Research, One Plan, Then Build

Plan a sandbox before you build it. Do the research and ask every
question first, put the answers in one plan, get that plan approved, then
build the sandbox in one pass. A sandbox built from questions answered
one at a time, halfway through the build, takes several tries. A sandbox
built from an approved plan should take one.

**Feedback.** Tell Cofactor the moment a step in this guide is wrong, unclear or missing; a command fails in a way the guide did not predict; you took a detour or had to guess; or a tool you needed does not exist. Send it when it happens, not only at the end: one sentence on what happened and what would have helped. Run `cofactor feedback "<what happened; what would have helped>" --topic instructions --command "cofactor sandbox guide <section>"`, or call the `cofactor_feedback_submit` MCP tool with the same text, topic and command. Never include secrets, customer data, real names or URLs; use neutral examples (Acme, example.com).

Use this whenever you are about to create a sandbox for an application
that has none: a first setup, a new application in a monorepo, or an
application moving to Sandboxes v2. New to Sandboxes v2? Read `overview`
first for what a sandbox is.

## 1. Enter plan mode

If your coding agent has a plan mode (Claude Code's plan mode, for
example), enter it now. If it has none, write the plan as your reply and
stop; do not start building until the human has answered.

## 2. Research before you ask

Answer every question you can from the code and from Cofactor's guides.
A question the repository settles is not asked.

From the repository: Dockerfiles and compose files, package manifests,
`.env.example` or the env schema, migrations and any seed scripts, the
auth setup (how users sign in, which roles exist), ports, background
workers and queues, and third-party SDKs.

From Cofactor's guides (`cofactor sandbox guide <topic>`): `feasibility`
(each dependency's verdict and whether a hermetic sandbox is possible),
`emulation` and `object-storage`, `personas` and `authentication`,
`secrets` and `env`, `seeds-and-actions` and `environments`, `verify`,
and `upload` when the code is delivered as an image.

Ask the human only what research cannot settle: a choice between
reasonable options, something only they know (which test account, which
staging backend, which journeys matter), or a dependency the verdict
table marks `unsupported` or unknown. Ask all of it together, once, at
the top of the plan, each question with the answer you would pick and
why, so approving the plan accepts them.

## 3. What the plan contains

The plan goes from the code as it is to a running sandbox someone can
sign in to. Every item below is in it, even when the answer is "none":

- Source and build: GitHub (repository, branch or commit, definition
  directory) or a pushed image (the exact build command, below).
- Services and dependencies: each service with its start command and
  health check, and each dependency with its verdict and file that shows
  it (database, queue, cache, workers).
- Ports: which are declared and which one is the app's web address.
  Each public port of each copy has its own host (`<label>.sbx.<zone>`,
  see `ports`), so say how every frontend bundle learns its API origin
  at serve time (a same-origin `/api` path, or a placeholder origin
  swapped in when the page is served); never a baked-in origin or a
  hostname pattern.
- Env vars and secrets: each variable the sandbox sets, which test keys
  are needed and from whom, and for each outside service whether it is
  emulated, run as a service, or called for real with a test key. Never
  a real credential.
- Seed data: what is created, through which of the app's own APIs or
  actions, and in what order.
- Personas: one per role, and how each gets a working login.
- Readiness: the health check each service answers, and what "ready"
  means for the app.
- What is infeasible: each `unsupported` or unknown item with the
  decision you propose (leave the journey out, disable the feature in
  the sandbox, stub it in the app, sign in another way). If the gaps
  make a hermetic sandbox infeasible, say so, and give the
  frontend-only fallback with the backend it would point at (never
  production; see `feasibility`).
- The exact commands you will run, in order, from the first validation
  to the last check.
- How you will verify the sandbox is running: the claim is ready, the
  web address serves, each persona signs in (`verify`).
- What the human must provide: each secret as the command they run in
  their own shell (never a key in chat), each approval, each decision.

## 4. Nothing is built until the plan is approved

Until the human approves, you do read-only research and nothing else:

- Reading files, and running commands that only read the machine
  (`docker buildx version`, for example).
- `cofactor` commands that only read: every `guide`, `setup status`,
  `whoami`, `application list`, `sandbox envs`, `sandbox show`,
  `sandbox secret list`.

Before approval, none of these:

- Writing `cofactor.sandbox.yaml` or any other file in the repository
  (draft the definition inside the plan; its first `sandbox validate`
  is after approval), or committing or pushing.
- Any command that creates something on Cofactor: `sandbox claim`,
  `sandbox verify-env` (it claims), `sandbox connect`, `sandbox push`,
  `sandbox refresh`, `sandbox sync`, `sandbox secret set`.
- Opening the Cofactor web app (`open`).
- Writing a secret, anywhere.
- Building or pushing an image.
- Creating an application. `connect`, `push` and `claim --from-local`
  all create one.

A GitHub connection already made in `cofactor setup guide source` stands:
the plan starts from it, and the human's approval of that connection is
the only one given before the plan. A command you are unsure about: check whether it creates something on
Cofactor, and treat it as building if it does.

## 5. After approval, build it in one pass

Carry out the plan as written, in its order, without stopping to ask;
the questions were answered in the plan. Fix a failing step inside the
plan's scope (read `troubleshooting` first) and keep going. The plan is
done when its own verification passes, not when a claim comes up.

Go back to the human only when an assumption in the plan turns out
wrong: a dependency the research called emulated is not, a persona
cannot sign in the way the plan said, a service needs a key nobody
listed, or the hermetic sandbox is not feasible after all. Say which
assumption was wrong, what you found and what you propose, then wait.
Do not change a decision the plan recorded on your own.

## Pushed images

A pushed image needs Docker with buildx, a build context and an exact
build command, all stated in the plan; `cofactor setup guide source`
has them, and `upload` says what the image must be. A GitHub-delivered
application is never built in local Docker: the claim is its build.
