# First sandbox

## First sandbox: the application and its first running copy

Learn the platform from `cofactor sandbox guide` and its topics, never from memory. This section is the order of work; the guide has the details.

Plan first, then build. The procedure is `cofactor sandbox guide plan`: read it now, and follow it here.

0. Tell the human what you are doing as you go (`overview`, "Progress page"): `cofactor setup update --step sandbox "<what you are about to look at>"` before the research, then post what you found in one sentence.
1. Plan. Before this, post `cofactor setup update --step sandbox "Drafting the sandbox plan."`. Enter plan mode if your coding agent has one; otherwise write the plan and stop. Until the human approves it, only read-only research is allowed (`plan`, section 4: no files written, no claim, no secret, no image, no application created).
   - Application name: an existing one from `cofactor application list`, or a new name from their code (the product's name). Do not create it: `connect` or `push` below creates it, with its sandbox as the default environment.
   - Feasibility: `cofactor sandbox guide feasibility`, then the other topics `plan` lists (`upload` and `source` for an image). The plan builds a hermetic sandbox unless it is infeasible or the human says otherwise.
   - Put in the plan everything `plan` says it contains. If the app is infeasible hermetically, or the human asked for frontend-only, the plan says so with the topic's table and your recommendation; for frontend-only, name the backend you would point at and ask them to confirm it is not production and holds no real customer data. Never take a backend URL from code unconfirmed.
2. Ask. All the questions research could not settle go to the human together, once, at the top of the plan, each with your proposed answer. Then one plan approval. Those are the only asks in this section. Write nothing and run nothing that builds until they approve. The moment they approve, post `cofactor setup update --step sandbox "The plan is approved; building the sandbox."` (no `--done` yet: that comes after sign-in is verified).
3. Build it in one pass, as approved. Write `cofactor.sandbox.yaml` in the guide's "What to build first" order: a service with a health check, one persona per role, one seed through the app's own API. Failures and surprises follow `plan`, section 5.
4. Source (the plan's choice):
   - GitHub: `cofactor sandbox validate`, then follow `cofactor sandbox guide cli`, "Setting up a new GitHub application" (it proves the definition with a claim before anything is merged), or merge the definition to the default branch and run `cofactor sandbox connect --application <app> --repo <owner/name>` (`--dir` if it is not at the root).
   - Image: `cofactor sandbox validate --upload`, build for `linux/amd64`, then `cofactor sandbox push --application <app> <image> --definition cofactor.sandbox.yaml --tag nightly --wait`. It prints one `Sync:` line: `syncable` means `cofactor sandbox sync` works on the image; `not syncable` names what to fix (the definition must sit at the image's WORKDIR, as `COPY . .` gives you) and the push still succeeds; `not checked (...)` is only advice skipped, so go on.
   - Image, nightly pushes: give the human the complete CI workflow in "Nightly push from GitHub Actions" in `cofactor sandbox guide upload` (its last step needs two more key scopes: `test-run:run` and `test-run:read`). `cofactor api-key scopes` hides the sandbox scopes unless Sandboxes v2 is on for the organization, but `cofactor api-key create` accepts them. CI signs in with an API key in `COFACTOR_API_TOKEN`; the secret is printed once, so never create it yourself. In the same handoff, give the human this to run in their own shell, from the repository (an organization admin, with `jq` and `gh`; for other CI store the secret in its secret store):

     ```bash
     TOKEN=$(cofactor api-key create --name "CI image push" --scope sandbox:read --scope sandbox:publish --scope property:read --json | jq -er .secret) && printf %s "$TOKEN" | gh secret set COFACTOR_API_TOKEN
     ```

     If the key was created but not stored, revoke it (`cofactor api-key list`, then `cofactor api-key revoke <id>`) and run it again.
5. Done when `cofactor sandbox envs --application <app>` shows a current version. If the app needs test keys, do the SECRETS part of `secrets-and-preview` first (the plan listed them) so the first claim builds with them. Then claim once to prove it: `cofactor sandbox claim --application <app> --wait`, and run the plan's verification: the URL serves and each persona signs in (see `cofactor-browser-testing`). Post what happened as you go with `--step sandbox`, one sentence each when you connect the code, create the application and claim the first sandbox. Show the human the URL and logins in the report; the blockers were decided in the plan, so there is no preview question unless a plan assumption turned out wrong (`secrets-and-preview`). Next is `verify`: sign in as each persona on this running sandbox and post `sandbox` done, which is what opens the app for the human. Keep this proving claim running through `tests`, `nightly` and `report`: `Sandbox running` is done only while a ready sandbox exists, so destroying it now would undo that check.

Secrets: never take a key in chat or write one into a file. The human sets them in their own shell (see `secrets-and-preview`).

Re-read: `cofactor setup status --json` shows Application created, Sandbox claimed and Sandbox running as done while the claim is alive. Destroy the claim (`cofactor sandbox destroy <id>`) only at the end of `report`, once the first tests no longer need it, and tell the human it will then show as not running.

Next: `cofactor setup guide verify`
