# Set up Cofactor with a coding agent

## Goals

1. Set up a hermetic sandbox environment, as hermetic as the app allows: the app's own seeded data, a working login for each role, and its databases, queues and third-party services inside the sandbox or emulated. `cofactor sandbox guide feasibility` says what the app allows; when a hermetic sandbox is not feasible, `first-sandbox` covers the frontend-only fallback, pointed at a non-production backend.
2. Set up the first tests: the human's existing Playwright tests or a first regression suite, running nightly with a failure alert, and run once.
3. Recommend Cofactor skills and install the ones the human picks, for yourself, the coding agent driving this setup. MCP is a question for the human ("Do you prefer Cofactor through MCP or the CLI?"): the CLI is the default, and nothing in setup needs MCP.

Goals 1 and 2 are the point of the setup; goal 3 is optional. Setup is finished when the report at the end says what happened to each.

**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 setup 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).

## What Cofactor is

Cofactor is an end-to-end testing tool for web apps. It runs browser tests against the app: the human's existing Playwright suite, or Cofactor tests written with Cofactor and kept working by it. They run in sandboxes, each a disposable copy of the app that Cofactor boots from their code (a GitHub repository or an uploaded image). By default a sandbox is hermetic: it has its own seeded data and a working login for each role, and the app's databases, queues and third-party services run inside it or are emulated, so every run starts from the same known state and does not touch production data. A few calls to a real provider may still leave the sandbox, with a test key; `cofactor sandbox guide feasibility` covers which. When a hermetic sandbox is not feasible, first-sandbox covers a frontend-only one pointed at a non-production backend.

This setup works through the three goals above in order: you install the CLI, sign in, connect the code, describe how to boot the app in `cofactor.sandbox.yaml`, prove one sandbox comes up and verify it (goal 1). Then you ask the human what they want to do first, from our two recommended paths: bring in their existing Playwright tests, or set up a first regression suite. Either way the suite runs nightly with failure alerts to Slack or email, and you run it once (goal 2). You recommend skills and install the ones the human picks (goal 3), and ask whether the human prefers MCP to the CLI. Only then do you report and open the web app. The end state is that nightly suite, with alerts on and a first run the human has seen.

Set up Cofactor end to end from the terminal. The human does not use a browser until the final report step, except for the approvals a step requires (GitHub's app approval, and `cofactor login` for accounts that need it). No credit card: a new account starts on the Free plan, with no end date.

You are the agent. Do the steps in order. Fetch one section at a time:

```bash
cofactor setup guide <section>
```

An unknown section exits 1 and lists the valid slugs. Do not run `cofactor setup guide` with no section: it prints every section at once.

## Sections, in order

- `overview`: This page: the table of contents and the rules.
- `install`: Check or install the CLI.
- `account`: Sign up or sign in with an emailed code.
- `workspace`: Pick or name the organization; MFA accounts and accounts that must use `cofactor login` use the browser login.
- `source`: Connect the code: GitHub (one browser approval) or an uploaded image.
- `first-sandbox`: Plan the sandbox (`cofactor sandbox guide plan`), then, once the human approves, create the application and claim its first sandbox (consult `secrets-and-preview` from here when the app needs keys).
- `secrets-and-preview`: Test keys and a checked preview; used from `first-sandbox`.
- `verify`: Sign in as each persona on the running sandbox, then re-read state with `cofactor setup status`; never trust exit codes.
- `tests`: Post `cofactor setup update --step tests "<one sentence>"` as you go; Cofactor marks it done once the first tests exist. Research whether the app has Playwright tests, then ask what to do first: bring in the existing tests, or set up a first regression suite.
- `nightly` (the `schedule` step): Whichever path they chose, schedule that suite nightly, add a Slack or email failure alert (post `cofactor setup update --step schedule "<one sentence>"` as you go; Cofactor marks it done once both exist), and run it once.
- `skills`: Optional: read Cofactor's skills, recommend some, and install only the ones the human says yes to; ask once whether the human prefers MCP to the CLI, and register MCP only if they do.
- `report`: The last step. Tell the human what is connected and what is pending, including the suite, its first run and the nightly alert, share the run, invite teammates, then `cofactor open`.

Any time:

- `troubleshooting`: Error codes and what to do.

## First check (idempotent, run it before anything else)

```bash
cofactor --version
cofactor setup status --json
```

`setup status --json` is the authority on being signed in: it calls the API, so a 401 or a session error means the session is dead or missing. Signed out, it fails: that is expected, start at `install`.

If it succeeds, skip install, account and organization: go to `source`. It has a `next` field and a `checks` list: resume at the first pending check, and do not redo done ones. Safe to re-run at any time.

(`cofactor whoami` is offline and only checks that the saved token has not expired; use it to read the user and organization, not to prove a session works.)

## Progress page

Right after you sign in, give the human the tracking link and say: "Open this to watch my progress." It is `trackingUrl` in the `cofactor signup verify --json` output: a signed-in link that works once, within 24 hours, so the human needs no password (an expired or used one: `cofactor open --no-open` prints a fresh 5-minute link). After `cofactor login` the human is already signed in in the browser: use `trackingUrl` from `cofactor setup status --json`, the organization address. The page lists the setup steps with what each is doing, and shows your updates.

Post a short update when you start a stretch of work and when it finishes. The page shows only your latest message, under the step you are on, replaced by the next one:

```bash
cofactor setup update --step <id> "<one sentence>" [--done]
```

- Steps: `connect-agent`, `sandbox` (create and verify the first sandbox), `tests` (create or import the first tests), `schedule` (schedule the first suite and add an alert).
- One plain sentence, at most 200 characters ("Found a Next.js app with a Postgres database and two user roles."). No secrets, tokens, codes, passwords or email addresses, and no customer data.
- Only `sandbox` accepts `--done`, and only once you have seen each persona sign in on the running sandbox. Cofactor marks `connect-agent`, `tests` and `schedule` done itself, so post a sentence without `--done`.
- Setup finishes for the page, and it opens the app for the human, when you post `sandbox` done after the first sandbox is running, or 15 minutes after the sandbox was ready, whichever is first. That is the page, not you: keep going through `tests`, `nightly`, `skills` and `report`. Updates keep working after that, and after the human skips the page.

## Ask the human only for

1. their email address
2. the 8-digit verification code from the email
3. accepting the Terms and Privacy policy
4. a browser approval (GitHub), when a step needs it

plus the single question each section names: the organization name, asked before the code is verified (`account`, `workspace`), GitHub or image (`source`), what to do first, existing Playwright tests or a first regression suite (`tests`), which Slack channel or email gets failures (`nightly`), and, in `skills` (optional), which recommended skills to install and whether they prefer MCP or the CLI.

Creating the first sandbox is the exception to asking one thing at a time. Research the app first and answer everything the code and the guides can answer. Whatever research cannot settle goes to the human together, once, inside the plan (`cofactor sandbox guide plan`), each with the answer you propose and what you assumed. The human then gives one plan approval, and nothing is built before it. The plan's questions and that one approval are the only asks in `first-sandbox`; the commands you hand the human (their own secrets, a browser approval) are actions from the approved plan, not questions.

Everywhere else, ask one thing at a time, and propose every other answer yourself.

## Rules (do not break these)

- Never print or paste a token or a verification code into a file, a commit or chat logs. Pass a code only as the `--code` argument; never echo it back.
- An exit code of 0 is not proof. After each step, re-read state with `cofactor setup status --json`.
- Never guess a code. A wrong code counts toward a lockout of 5 tries.
- Do not fake a TTY (no `script`, `expect`, pty wrappers). Use the flags in this guide.
- Show the Terms and Privacy links to the human and get their yes before `--accept-terms`:
  - Terms: https://runcofactor.com/terms
  - Privacy: https://runcofactor.com/privacy
- You open no browser until the final `report` step, except the required approvals: GitHub's app approval, and `cofactor login` for accounts that need it. Do not run `cofactor open` earlier, except `cofactor open --no-open` to replace an expired tracking link. The human watches your progress on the tracking link (see "Progress page"); that is their browser, not yours.
- Use neutral example names (Acme, example.com) in anything you write.
- If something fails on Cofactor's side, stop and give the human the exact command, the error and the time.

Next: `cofactor setup guide install` (already signed in: `cofactor setup guide source`)
