# init — Scaffold a Shiplight test project + write context

Greenfield setup: scaffold the Shiplight project files, then discover and write
`specs/context.md`. Run once per project. `cover` invokes this as conditional
step-0 on the Shiplight-format branch; `create-yaml-tests` and `fix` assume an
initialized project.

> **Confirm before scaffolding a non-empty project.** Scaffolding merges into an
> existing repo — never overwrite the user's files; resolve conflicts (below).

## Read first

- `_shared/project-layout.md` (canonical layout + edit contract)
- `_shared/knowledge.md`, `_shared/secrets.md`
- `_shared/package-manager.md` (npm or Bun commands; decide before scaffolding)

## 1. Identify the project root

All paths are relative to the Shiplight test project root. If it's not clear, ask
the user to confirm before creating or moving files.

## 2. Scaffold

Run the launcher’s `init . --json` command from the project root using the
[package manager guide](./_shared/package-manager.md). It
writes any missing files and prints a single JSON object on stdout. Parse it:

- `files_needing_agent_merge` — one entry per conflict; resolve every one (below).
- `env_setup_state` — which `.env` situation you are in, and in particular
  whether overwriting an existing `.env` is safe. Step 3 still decides *which*
  credential to ask for.
- `name_was_normalized` — the package name was rewritten; tell the user, and
  offer `--name <name>`. JSON mode prints no notice, so this field is the only
  signal.
- `error` — the run failed and the object contains nothing else.

Pick the scaffold's manager by the rule's scaffolding case in
`_shared/package-manager.md`: the user's explicit choice, else the project root's
own `packageManager` or lockfile, else the launcher (`npx`, or `bunx` when npm is
not installed). Never walk up here; a lockfile in a parent folder does not count.
In a Bun project run the launcher with Bun. The Bun scaffold writes Bun scripts, `packageManager`, a direct
`@playwright/test` dependency and a Bun `.mcp.json` entry. When the user named a
manager and the folder has no `package.json` yet, first write the minimal
`package.json` the rule's step 1 describes, so a lockfile of another kind there
cannot override the choice.

The launcher reuses the selected CLI for existing projects. It bootstraps
`shiplight@latest` for new projects.

Do not pre-create empty directories — create them only when you have content to
place in them.

### Resolve scaffold conflicts

When the target was empty, `files_needing_agent_merge` is empty — proceed. When the
user already had files, it contains one entry per conflict. For each entry: Read
the file at `abs_path`, apply the change described by `merge_strategy` and
`instructions` using the supplied `template` (and `lines_to_ensure` / `merge_key`
when present) as the source of truth, and write the merged result back with Edit
(preferred) or Write — **never delete the user's existing content**.

| Strategy | Typical file | What to do |
|----------|--------------|------------|
| `json_merge_deps_and_scripts` | `package.json` | Add missing deps + `test`/`test:headed` scripts, and `packageManager` when the template has one and the file has none. Don't change `name`, `version`, or other fields. Ask before flipping `type` to `"module"`. |
| `append_missing_lines` | `.gitignore` | For each line in `lines_to_ensure`, append if absent. Group under a `# Shiplight` block. |
| `json_merge_under_key` | `.mcp.json` | Add the template's entries under `merge_key` (e.g. `mcpServers`). Don't overwrite a server name the user already has. |
| `append_missing_env_keys` | `.env.example` | For each `KEY=` line in the template, append (preserving commented form) only if `KEY` isn't already mentioned. |
| `review_and_decide` | `playwright.config.ts` | Show the user the template; ask whether to replace, merge `...shiplightConfig()`, or leave alone. Don't modify without confirmation. |

Resolve every entry before `npm install` — a skipped merge usually leaves the
project unable to run Shiplight tests. Then run `npm install` (in a Bun project,
`bun install`). If `package.json` still has no `packageManager`, record the
manager you scaffolded with, as the rule says after scaffolding.

## 3. Configure the credentials the chosen tests need

A test made only of deterministic YAML steps (explicit locators and assertions)
makes no model-provider calls. Do not ask for provider credentials just to run one.
AI-powered steps call Shiplight's hosted model service and need a Shiplight API
token. Eligible new organizations can try the public hosted service with free
sign-up credit; onboarding shows the current offer and eligibility. During the
sign-up-credit period, hosted AI steps also need a verified model-provider key
unless the organization has a card on file, has made a paid top-up, or has AI
access enabled by its operator. When set, the key is sent to the configured
planner with each planner request; the public hosted service uses HTTPS, and
self-hosted planners must use HTTPS except on loopback. Use only a planner
endpoint you trust. The hosted service uses the key only to verify access for a
no-card organization. For each hosted AI request from a no-card organization,
Shiplight makes a provider call that may be billed; Shiplight usage also draws
on the organization's credit. Verification questions use the step instruction,
on-screen elements, and email body when present; the question is truncated at
32,000 characters. For no-card organizations,
Shiplight stores the question and provider's answer to improve prompts and
fine-tune models. Variable values of 3 or more characters are replaced wherever
they appear; shorter values are replaced only as whole tokens. Email addresses
are masked throughout the question. In email bodies only, HTTP/HTTPS links and
4-or-more-character codes containing a digit are masked. Spaced numeric codes
are also masked when they have at least two groups of 2-or-more digits separated
by one space. Other screen and instruction text may remain. The answer is
stored as returned and may repeat text or variable values. The provider key is
not recorded. Don't
use this option if that text must not be sent to the planner and provider.

Ask whether the user wants AI-powered steps or deterministic steps only. If they
want AI steps, explain that the API token connects the project to Shiplight, and
ask whether they will supply a provider key or use another available hosted-AI
payment option. Never ask them to paste a secret into chat. Since editing `.env`
normally requires the user's explicit ask (`_shared/project-layout.md`), this
step is that ask — but still confirm before writing a credential.

- **Shiplight API token**: first make sure `SHIPLIGHT_API_URL` and
  `SHIPLIGHT_WEB_URL` are in the shell or `.env`. The command creates the token at
  `SHIPLIGHT_WEB_URL`, and tests send it to `SHIPLIGHT_API_URL`. There is no
  default host: if either is missing, stop and ask the user for their backend's
  URLs, never guess them, and with their OK add them to `.env`
  (`SHIPLIGHT_API_URL=<url>`, `SHIPLIGHT_WEB_URL=<url>`).
  Shiplight's public service uses `https://api.shiplight.ai`; its web app is
  the public service's `https://app.shiplight.ai`. They are not defaults. Use
  them only if the user explicitly chooses the public hosted service. If neither URL is
  configured, confirm the URLs with the user before adding them. Onboarding
  shows the current sign-up credit offer and eligibility.
  Then run `npx shiplight setup-api-token` (in a Bun project,
  `bun run --bun shiplight setup-api-token`) yourself via Bash — it opens
  the user's local browser for device-auth approval on its own (no terminal
  interaction needed from them), polls until they approve, then creates the token
  and writes `SHIPLIGHT_API_TOKEN=...` to `.env` automatically. Tell the user to
  check their browser and approve; use a generous timeout since approval can take
  a few minutes. Don't hand-write the token into `.env` yourself — the command
  owns that write. The device-auth session is not persisted; only the personal API
  token remains. There is no CLI logout command: remove `SHIPLIGHT_API_TOKEN` from
  `.env` to stop using it in this project, or revoke the token at
  `$SHIPLIGHT_WEB_URL/api-tokens` to invalidate it everywhere.
  Closed CLI releases before 0.1.103 need an update for this command.
  If the hosted service returns a 401 saying the token is unknown, run the
  command again against that service.
  On the hosted service, a token alone does not satisfy the payment requirement
  for AI steps. An eligible sign-up-credit organization can run hosted AI without
  a card by supplying a valid provider key; otherwise, a saved card, paid top-up,
  or operator-enabled access is needed. A 402 `not_entitled` reply includes a
  message describing the issue and, when configured, a Billing link for the owner.
- **Model-provider key**: supported variables are `ANTHROPIC_API_KEY`,
  `OPENAI_API_KEY`, `GOOGLE_API_KEY`, and `GEMINI_API_KEY`. Tell the user which
  variable to set. Do not request or accept the secret in chat; have the user
  add it to `.env` locally. When set, the key is sent to the configured planner
  with each planner request. For no-card organizations, Shiplight makes a
  provider call for each hosted AI request, which the provider may bill.
  For no-card organizations, Shiplight stores verification questions and
  provider answers to improve prompts and fine-tune models. Questions include the
  step instruction, on-screen elements, and email body. Variable values of 3 or
  more characters are replaced wherever they appear; shorter values are replaced
  only as whole tokens. Email addresses are masked throughout the question. In
  email bodies only, HTTP/HTTPS links, spaced numeric codes with at least two
  groups of 2-or-more digits, and 4-or-more-character codes containing a digit
  are masked. Other screen and
  instruction text may remain. Answers are stored as returned and may repeat
  text or variable values.
  The key itself is never recorded. Don't use this option if that text must not be sent to the planner
  and provider; the AI step runs on Shiplight's models and charges the
  organization's credit.
- **No AI steps**: proceed without a model-provider key. Keep tests deterministic
  with explicit locators and assertions. A Shiplight token is only needed for
  hosted extensions or Cloud features the user chooses to use. If a provider key
  is already set, the CLI still sends it to the configured planner with planner
  requests; unset it when it is not needed.

Confirm `.env` is git-ignored (the scaffold's `.gitignore` merge already adds it)
before moving on.

## 4. Discover & write `specs/context.md`

Understand the application, user goals, risks, target deployment, auth needs, and
data strategy. Before asking questions, scan available context: existing
`specs/context.md` and `specs/tests/`; codebase routes, components, framework, and
`package.json`; the git branch diff; existing tests; README/PRDs/docs; existing
`knowledge/` notes.

Write or update `specs/context.md` with:

- **App profile** — name, framework, key pages and features
- **Risk profile** — what matters most and what is fragile
- **Testing scope** — in-scope and out-of-scope areas
- **User roles** — roles and permission levels to cover
- **Data strategy** — how test data is created and cleaned up
- **Targets** — base URLs, auth method, special setup
- **Known facts and decisions** — durable preferences and constraints
- **Open questions** — unresolved or stale questions

Do not store raw secrets (`_shared/secrets.md`).

## Next

Suggest per SKILL.md's "After a subcommand completes" table — typically `setup-test-auth`
when the app has login, else `cover` to drive the full create-flow.
