---
name: multi-agent-test-gen
description: "Write the tests an app lacks, stage by stage, each gated on its artefact: inventory, read-only gap analysis, handles, tests, runs, per-test classification. Use when screens, flows or a change need tests."
user-invocable: true
argument-hint: "[scope] [--platform ios|android|web] [--repo <path>]  -  scope: a screen, route or flow, or empty for what changed since the last test-gen run"
metadata:
  language: en
  not-for: test, bug-bash, scenario-audit, manual-test
---

# multi-agent-test-gen

**Input**: $ARGUMENTS

Writes the tests an app is missing, in the repo's own frameworks, and reports
what they found. Seven stages run as subagents, each leaving one artefact that a
script gates before the next stage starts: inventory, discovery, gaps, handles,
write, run, classify. The two analysis stages run as a read-only agent type, so
nothing they do can change the repository.

Stage contract, read before Step 1:

```
$HOME/.claude/multi-agent-refs/test-gen/stages.md
$HOME/.claude/multi-agent-refs/features/test-classification.md
```

Scripts: `test-gen-gate.mjs` (artefact, handle and write gates),
`handle-diff-audit.mjs` (handles only), `test-classify.mjs`,
`test-gen-state.mjs` (change base, revision cap), `evidence-gate.mjs`, all under
`$HOME/.claude/scripts/`. Schemas: `test-gen-inventory`, `test-gen-gaps`,
`test-gen-results` under `$HOME/.claude/schemas/`.

## Gotchas

- An analysis stage that wants to "just fix" something is out of its lane. Discovery and gaps go to `test-gen-analyst`, whose grant is an allowlist of read-only tools.
- The handle stage adds handles and nothing else. A wrapped view, a renamed element or a changed condition fails `handle-diff-audit.mjs`; revert the hunk, do not argue with the audit.
- `HANDLES: none-needed` is a claim that the handles exist. It needs the `file:line` of each.
- A test that passed once is not green. Unstable is a fail, and a red test with no base to compare is `not_measured`, not a regression.
- Test data is named in `dataNeeds[]`, never filled in by the run. A credential is a `secretRef`.
- A product bug is reported, never fixed here. In autopilot it parks the run.

## Step 1 - Prepare

1. Repo: `--repo`, else the current directory; confirm it in one line.
   Platform: `--platform`, else detect as `multi-agent-test` does. One
   timestamp names the run everywhere: `TS="$(date +%Y%m%d%H%M%S)"`, run id
   `tg-$TS`, `RUN="$HOME/.claude/logs/test-gen/<repo>/$TS"`, created with
   `mkdir -p "$RUN"`.
   Register the run so its progress shows wherever runs are read (the office
   panel, `multi-agent-status`), with one step per stage below:

   ```bash
   TG_ID="tg-$TS"
   S="${LOGS_ROOT:-$HOME/.claude/logs/multi-agent}/$TG_ID"; mkdir -p "$S"
   printf '{"taskId":"%s","mode":"test-gen","status":"in_progress","currentPhase":0}' "$TG_ID" \
     | node "$HOME/.copilot/scripts/write-state.mjs" "$S/agent-state.json" >/dev/null
   export MULTI_AGENT_TASK_ID="$TG_ID"
   bash "$HOME/.copilot/scripts/phase-tracker.sh" init "$TG_ID"
   for p in "0:Prepare" "1:Inventory" "2:Discovery" "3:Handles" "4:Tests" "5:Report"; do
     bash "$HOME/.copilot/scripts/phase-tracker.sh" add "${p%%:*}" "${p#*:}"
   done
   bash "$HOME/.copilot/scripts/phase-tracker.sh" update 0 in_progress
   ```

   Each step below starts with `update <N> in_progress` and ends with
   `update <N> completed` (Step 5 and Step 6 share step 4), every call prefixed
   `MULTI_AGENT_TASK_ID=<the tg- id, written out> MULTI_AGENT_STRICT_TASK_ID=1`.
   There is no task widget here: call `phase-tracker.sh render` after each
   change, and close the record with `"status":"complete","currentPhase":5`
   through the same `write-state.mjs` call when the report is written.
2. Toolkit: `multi-agent:test_results_classify`, `multi-agent:coverage_gap` and `multi-agent:ui_locator_codegen`
   need `@mmerterden/multi-agent-mcp` >= v3.25.0. Check the tool list
   once. Each missing tool has a fallback in `stages.md` (classify from the
   repeats, the analyst matches coverage itself with every match `inferred`,
   locators written by hand from the handles); say which fallbacks are in use,
   once.
3. Change base. `TG_STATE="$HOME/.claude/knowledge/<project>/test-gen-state.json"`:

   ```bash
   node "$HOME/.claude/scripts/test-gen-state.mjs" base-scan --state "$TG_STATE" --repo "$REPO" \
     --graph "$HOME/.claude/knowledge/<project>/code-graph.json" > "$RUN/base-scan.json"
   ```

   Exit 0: the changed files and their dependents narrow an empty scope. Exit 1
   (`measured: false`, first run or base gone): nothing is narrowed, and the
   reason goes in the report.

## Step 2 - Inventory and priority

Stage 1 of `stages.md`, then the priority pick: ask which items to cover
(one question to the user; autopilot: the top three, recorded as `autopilot-top3`). Gate:

```bash
node "$HOME/.claude/scripts/test-gen-gate.mjs" check --stage inventory --file "$RUN/inventory.json"
node "$HOME/.claude/scripts/evidence-gate.mjs" --claim artefact --status passed \
  --evidence "$RUN/inventory.json" --stage-start "$STAGE_START"
```

## Step 3 - Discovery and gaps (read-only)

Stages 2 and 3, both with `subagent_type: "test-gen-analyst"`. Write each answer
with the Write tool, then gate each file with `evidence-gate.mjs --claim
artefact` against its own stage start, and each with `test-gen-gate.mjs check`
(`--stage discovery`, `--stage gaps`). Show the gaps (bucket, priority,
expectation) and the `dataNeeds[]`; ask for the data or the credential names
(autopilot: write only the scenarios whose needs are met).

## Step 4 - Handles

Stage 4. Snapshot first (`test-gen-gate.mjs baseline`), then the subagent, then
both gates against that snapshot: `test-gen-gate.mjs handles` and
`handle-diff-audit.mjs --baseline` exit 0. The gates read only the stage's own
change; the person's uncommitted work is never the stage's to revert. Skip the
stage when no gap has `handlesNeeded`, and say so.

## Step 5 - Write, run, classify

Stages 5 to 7: a new snapshot, the tests and `written.json`, the
`test-gen-gate.mjs written --baseline` gate (test files only, inside the repo
and changed by the stage, no test gaming, a discovered runner, more than zero
tests run), the repeated runs, then
`results.json` through `test-classify.mjs` and `test-gen-gate.mjs results`.

## Step 6 - Triage

Stage 8, one verdict per non-pass result, each through `test-gen-state.mjs
revise`. Exit 3 parks the run: stop, keep every artefact, report why, and record
no change base, because the run did not finish.

## Step 7 - Report and base

Print, in this order: the tests written (file, scenario, expectation), the
results table from `results.json` with every `fail` and `not_measured` reason,
the triage verdicts, the handles added (`handles.md`), the scenarios blocked by
data, and `gaps.json`'s `unmeasured[]`. State which parts are mechanical (the
gates, the classification, the handle audit) and which are judgement (the
inventory, the gaps, the triage). Then record the change base
(`test-gen-state.mjs base-record`).

Offer, one question: open the product bugs through `multi-agent` with each
failing test as the acceptance check, or stop. **No commit, no push, no issue
from this command.**
