# Seed Workflows

Seed workflows provision test data that several tests share. A seed
workflow creates data and exports named variables that downstream
workflows can reference. Data unique to one test is created inline in
that test, not seeded (see the Test Data Lifecycle section).

## Why use seeds

- Reusable — the qualifying condition: one seed serves many test workflows
- Fast: seeds can use API sequences instead of slow browser interactions
- Clean: paired teardown workflows handle cleanup automatically, on
  pass and fail
- Isolated: each run gets its own fresh data via macros

## How seeds work

1. Create a workflow with flowType `"seed"`
2. Define `seedOutputs` — the named values the seed exports
   (e.g., `project_id`, `user_email`, `order_number`)
3. The seed creates data and captures those values during execution
4. Downstream workflows add a Seed node that references this seed
5. The seed's outputs are available as `$var.{dataKey}` in the test

Example:

```text
Seed "Create Test Project" exports: project_id, project_name
Test workflow references the seed, then uses:
  Navigate to $var.project_id
  Assert page title contains $var.project_name
```

## Creating seeds

A seed workflow shell needs a name, declared outputs, and usually a paired
teardown. Outputs are the contract downstream workflows depend on; define
them before building the seed body so every node captures the right values.

Simple data outputs need:

- `dataKey`: stable variable name, used as `$var.{dataKey}`
- `name`: human label
- `description`: optional context for builders and reviewers

Credential outputs additionally define `credentialOutput` — the field
mapping (`"$generate.email"` for a minted address), inbox behavior, and
`loginUrl`. The full recipe for a seeded user, from declaration to the
consumer's `from_seed` login and teardown, is the Seeding a Test User
section (`cofactor workflow guide seed-users`).

After creating the shell, add nodes through the Flow Designer, workflow sync,
or API, then publish once the seed and paired teardown both pass.

CLI users: see `cofactor workflow guide cli-seeds` for command usage.

## Building a seed: start with the browser

The easiest way to build a seed is to start with browser-based steps:

1. Create a seed workflow (flowType: `seed`)
2. Add a login node with credentials that have permission to create data
3. Add action nodes that create the data through the UI
   - Use macros for all names and identifiers (`$EMAIL()`, `$RANDOM_STRING(6)`, etc.)
4. Add `declaredOutputs` on action nodes to capture generated values
   (IDs, URLs, confirmation numbers)
5. Define `seedOutputs` on the workflow to export those captured values
6. Test and publish the seed

This approach lets you discover exactly what steps and data are needed
by interacting with the real application.

## Converting to API sequences

Once a browser-based seed works reliably, convert it to an API sequence
for speed and reliability. Cofactor can do this automatically.

Auto-conversion from a workflow run:

1. Open the seed workflow in the Flow Designer
2. Add an `api_sequence` node
3. In the node config, choose "Generate from workflow run"
4. Select a completed run of the browser-based seed
5. The AI agent analyzes the network requests captured during
   that run and generates an equivalent API sequence automatically
6. The agent tests each step against the live API to verify it works
7. Review the generated sequence and publish

The AI agent has access to your application's API catalog and can:

- Search for endpoints by keyword
- Inspect request/response schemas
- Test individual API calls to verify they work
- Compose a multi-step sequence with variable passing between steps
- Validate the full sequence end-to-end before finalizing

You can also generate an API sequence from a prompt — describe what
data to create and the agent will find the right endpoints and build
the sequence.

API sequences support two kinds of dynamic values:

- Step references: `{{stepId.response.path.to.value}}` to pass data
  between steps (e.g., use the project ID from step 1 in step 2)
- Macros: `$EMAIL()`, `$UUID()`, `$RANDOM_STRING(6)`, etc. for fresh
  unique data each run

Exports map response fields to seed output variables:

- `stepId`: which step to extract from
- `responsePath`: dot notation path (e.g., `"data.id"`)
- `variableName`: the `$var.{name}` downstream workflows will use

## Execution modes

Seeds automatically choose the best execution mode:

- `browser`: Runs through the browser like a normal workflow
- `api`: Runs the API sequence directly — no browser needed
- `recommended`: Uses the API sequence if one exists, otherwise browser

Once you've added an API sequence to a seed, it will automatically
run via API — faster, cheaper, and more reliable than browser execution.

Not all seeds can be converted — some data creation flows require
browser interaction (file uploads, complex wizards, third-party
integrations). Keep those as browser-based seeds.

## API call timeout

When a seed runs in API mode (executionMode `"api"`, or `"recommended"`
resolving to API), each HTTP call uses a 2-minute (120s) timeout by
default. Seeds/teardowns that hit slow provisioning endpoints can raise
this per seed node via `apiTimeoutSeconds` — it applies to both the seed
and its paired teardown. Ignored for browser-mode seeds.

Set it two equivalent ways:

- Flow Designer: the "API call timeout (seconds)" field on the Seed node
- workflow JSON (create / sync): on the seed node's `configJson`, e.g.

  ```json
  { "nodeType": "seed",
    "configJson": { "seedExecutionMode": "api", "apiTimeoutSeconds": 300 } }
  ```

Leave it unset to keep the 120s default; an invalid value falls back to
the default rather than erroring.

## Teardown workflows

Every seed can have a paired teardown workflow that cleans up the
data it created. Teardowns run automatically after the seed's
downstream workflows complete — whether they passed or failed.

- Set flowType to `"teardown"` and link it to the seed
- The teardown receives the same `$var.{dataKey}` variables the seed exported
- Use those variables to find and delete the created data

Teardowns are the cleanest way to handle cleanup — they keep cleanup
logic separate from test logic and run automatically.

## Sandbox-action-backed seeds

When the property has a verified sandbox template with actions, a seed
workflow can provision data by running a SANDBOX ACTION node instead of
driving the UI or composing API calls. The seed's body is a single
`sandbox_action` node (plus End); the action's outputs register as
`$var.{key}` and satisfy the seed's declared outputs directly.

- Fastest seed form: runs entirely server-side, no browser session
- Discover actions with `list_sandbox_actions`; outputs come from the
  action's `outputSchema`
- Constraint: consuming workflows become sandbox-only — on a
  non-sandbox environment the action node skips with a warning and
  produces no variables
- Teardown is usually unnecessary: sandbox environments are destroyed
  after the suite, taking provisioned data with them

## Seed best practices

- Use macros in all created data (`$EMAIL()`, `$RANDOM_STRING(6)`, etc.)
  so each run gets unique, non-conflicting data
- Keep seeds focused — one seed per logical dataset
  (e.g., "Create Project with Members" not "Create Everything")
- Export only what downstream workflows need — don't over-export
- Start with browser-based seeds, then use auto-conversion to generate
  an API sequence from a completed run
- For sandbox-backed properties, prefer a sandbox-action-backed seed
  (see above) — server-side, fast, and reliable
- Pair seeds with teardown workflows for automatic cleanup
- To share seed data across several workflows in one test suite,
  reference the same seed from each; order dependent workflows with
  suite layers (setup nodes are deprecated)
