# Environments (Flavors)

An environment (a "flavor") is the same sandbox — same code, same toolchain
— started with different data and config. `default` is required; every
definition must declare it. Previews use `default`, and so does `cofactor sandbox claim` when you don't pass `--environment`.

For a Workflow Run, `sandbox.reset` keeps the environment selected for that Run
and restores the exact snapshot and version of its active copy. It does not
switch flavors or move the Run to a newer environment snapshot. To test another
environment, start a Run configured for that environment.

## When to add a flavor vs. passing inputs in the test

Rule of thumb: add a flavor when many tests need the same starting state.
Call actions from inside the test when only that test needs the data.

In the example app, `default` seeds one workspace with an owner, a
member and a viewer, because nearly every test signs in to a workspace
as one of those roles. A test about archiving a project, on the other
hand, calls `create-project` itself (see the worked example in
`seeds-and-actions`) rather than getting a new flavor. If you find
yourself writing a flavor whose seeds only exist to serve one test,
that data belongs in the test's own action calls instead.

Add a second flavor only when real tests need a starting state `default`
can't give them cheaply — typically a different plan or set of feature
flags (see "What to build first" in `seeds-and-actions`). Don't add one
speculatively.

## Descriptions are for the reader

`description` is required and can be multi-line (YAML `|`). Write it for
the agent choosing between environments — say what's already there and
who can do what, not just a label:

```yaml
environments:
  default:
    description: |
      Workspace "Acme": owner is its owner, member a member,
      viewer a read-only viewer. No projects yet; create them
      with create-project.
```

A vague description ("the default setup") gives an agent nothing to
decide with. Say what exists, in terms of the ids and roles a test will
actually use.

## Extends

`extends: <other-environment-name>` composes one environment on top of
another. The child's snapshot runs the **parent's seeds first, then the
child's own**, in that order. `env` merges the same way: parent's keys
first, the child's last (so a child can override a parent's value).
Chains can be more than two deep; a cycle is rejected by `validate`.

Step ids must be **unique across the whole resolved chain** — a parent
and its child can't reuse the same id, since the child's steps can
reference the parent's outputs.

An environment carries its own `description`, `extends`, `seeds` and
`env`. It has no `params` or `personas` of its own: those belong to the
definition and are the same in every environment.

## Seed steps and `${id.key}`

Each `seeds` step has an `id`, exactly one of `action` or `operation`,
and an `input`:

- `id`: unique within the resolved chain; later steps reference this
  step's output as `${id.key}`.
- `action`: a declared action's slug, or `<plugin>.<op>` for an
  emulator operation (e.g. `email.sent-emails`).
- `operation`: a platform built-in, today only `database.fast-forward`
  (see `data-fast-forward` and `seeds-and-actions`).
- `input`: literal values, or `${id.key}` references to an earlier
  step's output.

A reference that is the **entire** value keeps the output's real type:

```yaml
input: { workspace_id: "${acme.workspace_id}" }   # exactly acme's workspace_id
```

A reference **embedded** in a longer string becomes a string
interpolation instead:

```yaml
input: { name: "Board for ${acme.workspace_id}" }  # becomes a string
```

`${id.key}` must point at a step that ran earlier in the chain and at one
of that step's *declared* output keys — `cofactor sandbox validate`
checks both and reports the path.

The example app's environments, in full:

```yaml
environments:
  default:
    description: |
      Workspace "Acme": owner is its owner, member a member,
      viewer a read-only viewer. No projects yet.
    seeds:
      - id: acme
        action: create-workspace
        input: { name: Acme, as: owner }
      - id: add_member
        action: add-member
        input: { workspace_id: "${acme.workspace_id}", persona: member, role: member, as: owner }
      - id: add_viewer
        action: add-member
        input: { workspace_id: "${acme.workspace_id}", persona: viewer, role: viewer, as: owner }
  free-plan:
    extends: default
    description: Same as default, but Acme is moved to the Free plan (3-project limit).
    seeds:
      - id: downgrade
        action: set-plan
        input: { workspace_id: "${acme.workspace_id}", plan: free }
  beta-features:
    extends: default
    description: Same as default, with the timeline view turned on.
    env:
      FEATURE_FLAGS: timeline
```

`add_member` couldn't run before `acme` — its `workspace_id` input
depends on `acme`'s output. Seeds always run in the order they're
written. `free-plan` sets its plan with a seed action because the app
stores plans in its database; `beta-features` uses `env` because the
app reads its flags from an environment variable (see the `env` topic
for which processes see it).
