# Component Dispatch (Phase 2 short-circuit)

<!-- toc -->
- [Entry conditions](#entry-conditions)
- [Plugin skill resolution](#plugin-skill-resolution)
- [Dispatch call](#dispatch-call)
- [Dispatch record (dispatch-layer owned)](#dispatch-record-dispatch-layer-owned)
- [Multi-repo report](#multi-repo-report)
- [Failure + resume](#failure-resume)
- [Cross-CLI behaviour (intentional divergence)](#cross-cli-behaviour-intentional-divergence)
<!-- /toc -->

> **TLDR**  -  When `taskType === "component"` (Figma URL in task description or instruction-driven figma workflow), multi-agent Phase 2 **does not run the TDD loop**. It delegates the entire phase to the enabled `ai-<platform>-toolkit` **marketplace plugin's** component skill (`create-component`, falling back to `create-ui-component`) via the Skill tool. Implementation lives in the plugin; multi-agent's job is classification, dispatch, and state report. The pipeline no longer bundles its own `figma-to-component` orchestrator  -  component skills live in one place, the plugin marketplace.

This doc is referenced from `$HOME/.claude/multi-agent-refs/phases/phase-2-dev.md`. Keeping it separate lets `phase-2-dev.md` remain tight (it's already the largest phase doc) and gives the orchestrator-report contract a stable URL for both Claude-side and Copilot-side implementations.

## Entry conditions

Phase 2 checks, in order:

1. `agent-state.json` has `taskType: "component"` (set by Phase 0 Step 7).
2. `state.componentScope` is set (same step).
3. The run carries a design reference  -  a non-null `figmaUrl`, a recorded
   `figmaAccess.tier`, or `instructionDriven`  -  and `figmaUrl` is non-null,
   because the plugin's create skill takes the URL as its argument. Phase 0
   Step 5 sets `instructionDriven` only alongside a `figmaUrl`, so the second
   half of this check is about a state that disagrees with itself.

All three → dispatch. A design reference with 2 or 3 unmet → **HALT.** Log the
anomaly to `agent-log.md` ("component dispatch expected but state incomplete:
<which field>"), write no `componentDispatch` record, leave Phase 2
`in_progress`, and stop with a user-visible error naming the missing field and
pointing at `phase0-exit-gate.mjs`.

**No reference at all** is the one component task with nothing to dispatch to.
Step 7 also classifies by diff shape  -  a new `Configuration.swift` beside a
`+Modifiers.swift`  -  and such a run has no frame anywhere. It records
`componentDispatch.outcome: "fell-through"` with its reason before the first TDD
step, then runs the TDD loop. `taskType` stays `component`, so Phase 3 still
applies the accessibility and token lens, which is what that classification buys.

A halt and a recorded non-dispatch are told apart by the record itself: a halt
leaves none and the phase open, while `fell-through` writes one and closes the
phase. The writer holds that line  -  `fell-through` on a run that does carry a
design reference is refused (exit 6, nothing written), because the generic path
on a task that has a design is skipping the Figma work: no token-compliance
check, no Code Connect publish, no component review, and artefacts that look
finished. The Phase 0 exit gate keeps a run from reaching Phase 2 without
`taskType` at all; these checks are the second line.

## Plugin skill resolution

Scope first, then platform. A screen and a single component are different jobs and
the plugin ships a skill for each; routing a screen to the component skill is why one
run produced entities and a mapper but left the screen half-wired.

| `state.componentScope` | Meaning | iOS skill | Android skill |
|---|---|---|---|
| `screen` (default when the frame is a full screen, or the task names a screen) | Full clean-architecture vertical: Entity → Repository → Mapper → UseCase → Copy → AnalyticsTracking → CoordinatorEvent → ViewModel → Scene → Preview, then verify | `ai-ios-toolkit:create-screen` | `ai-android-toolkit:create-screen` |
| `component` | One reusable UI component (Configuration / View / +Modifiers / Code Connect) | `ai-ios-toolkit:create-component` | `ai-android-toolkit:create-component` |
| `evolve` | Change an existing component | `ai-ios-toolkit:evolve-component` | `ai-android-toolkit:evolve-component` |

A `screen` scope never falls back to `create-component`: that is the mistake
the paragraph above describes. `smoke-dispatch-skill-names.sh` holds every name
in this table to the plugins checkout. `figma-validate` takes the scope
(`--scope screen|component|evolve`), so a screen-sized frame passes it on the
screen path.

```
web, multi-*      → HALT with clear error (no web target)
```

Phase 0 Step 7 sets `state.componentScope` alongside `taskType`: a Figma frame that
is a full screen, or a task whose title names a screen, is `screen`; a frame that is
a single atom is `component`. When it cannot be decided, ask  -  do not default to
`component`, because the screen path is a superset and the component path silently
omits the wiring.

**Pre-implementation validation is not optional on iOS.** Before the create skill
runs, dispatch `<toolkit>:figma-validate` (probed like every dual-name skill; a toolkit that ships none records the skip) for the frame. It checks
registry presence, Code Connect strategy, **design token compliance**, dependency
readiness, atomic scope and already-implemented status in about ten seconds. Those
are precisely the checks whose absence produced guessed spacing and an unpublished
Code Connect binding. A `figma-validate` failure halts the dispatch. The verdict
is recorded as `componentDispatch.validate` (next section), and the writer holds it.

**Dual-name resolution.** The public (`multi-agent-plugins`) and a corporate/private marketplace named the same skill differently  -  `create-component` vs `create-ui-component`. Dispatch tries `create-component` first; if it is not available in the current repo, tries `create-ui-component`. (Same dual-name rule applies when `taskType` maps to evolve → `evolve-component`/`evolve-ui-component`, or fix → `fix-bug`.)

If **neither** resolves, the platform's `ai-<platform>-toolkit` plugin is not enabled in this repo. **Halt with a user-visible error**: "component task requires the ai-<platform>-toolkit plugin enabled in this repo (`.claude/settings.local.json`)." Do not silently fall back to TDD  -  a component task ran through the bugfix path would produce wrong artefacts.

## Dispatch call

Emit `→ dispatching create-component <componentName>` via the progress contract, then invoke the resolved plugin skill via the **Skill tool** with:

- `figmaUrl`  -  primary argument (the plugin skill's `<figma-url>`)
- the component name + the analysis Section 6 (Bileşen Envanteri) row + Section 13.1 conventions as context (the plugin skill does not re-read the pipeline's analysis doc on its own  -  pass what it needs)

Plugin skills are user-facing lifecycle skills; they do **not** accept an `agentState` path and do **not** write `agent-state.json`. State tracking therefore moves to the dispatch layer (next section).

## Dispatch record (dispatch-layer owned)

Because the plugin skill does not write pipeline state, **multi-agent's dispatch layer owns `state.phases["2"].componentDispatch`**, not the skill. It is the phase's answer to one question: what did the dispatch layer do? Three outcomes, and they are not interchangeable.

```json
{
  "outcome": "dispatched",
  "skill": "ai-ios-toolkit:create-component",
  "buildStatus": "green",
  "testStatus": "green"
}
```

| Field | When | Meaning |
|---|---|---|
| `outcome` | always | `dispatched` (the skill ran), `skipped` (the skill resolved and was deliberately not run) or `fell-through` (the TDD loop ran instead) |
| `skill` | `dispatched` | The plugin skill that ran, verbatim |
| `reason` | `skipped`, `fell-through` | Why no dispatch happened |
| `buildStatus`, `testStatus` | on return | What the plugin reported |
| `validate` | `dispatched` on a Figma frame (`figmaUrl`, or Figma tier 1 or 2) | `{skill, verdict, reason?}`: the figma-validate pre-check, verdict `VALID`, `CONDITIONAL`, `INVALID`, `ALREADY_IMPLEMENTED` or `skipped` (with a reason, for a toolkit that ships none) |

`write-state.mjs` refuses to close Phase 2 on a `taskType: component` run without it (exit 6, nothing written). Five things are held there rather than in the schema, because expressing them needs a conditional the shipped validator does not implement:

- A `dispatched` outcome names a skill that appears in `telemetry.skillCalls[]`, so a silent skip cannot pass as a dispatch.
- It comes with `state.componentScope` set, and the skill is the one that scope routes to in the table above. A screen recorded against the component skill reads as a finished screen whose wiring was never built.
- A qualified skill name belongs to one of the platforms the table carries; a target outside them has no component skill to reach.
- A `skipped` or `fell-through` outcome gives a reason, and `fell-through` is refused outright when the run carries a design reference (Entry conditions).
- A `dispatched` outcome on a Figma frame (`figmaUrl`, or Figma tier 1 or 2) carries `validate`: a figma-validate skill that appears in `telemetry.skillCalls[]`, a verdict other than `INVALID`, and scope `evolve` when the verdict is `ALREADY_IMPLEMENTED`; or verdict `skipped` with a reason.

Granularity is coarse on purpose: one record per phase, not the old 3.0-3.8 breakdown, because the plugin reports at its own granularity. For progress WHILE the phase runs, write `state.phases["2"].subStep` (a short token) so a resume redoes only the unfinished tail. The visual sub-phase rows the tracker renders live in `tracker-state.json`, which `phase-tracker.sh` owns; they are presentation, not run state.

## Multi-repo report

When multi-repo mode is active (`state.projects.length > 1`), the dispatch layer passes `state.projects[]` to the plugin skill and sequences writes across the repo graph:

- Tokens, accessibility metadata, testing identifiers, localisation keys → `repos.common`
- View / Configuration / Modifiers / Preview → `repos.components`
- Wiki markdown → `repos.wiki` (if `wiki.mode === submodule` or `separate-repo`) or `repos.components/.wiki/components/` (if `wiki.mode === in-repo`)

Multi-agent does **not** split the component task into per-repo Phase 2 runs  -  the dispatch layer sequences the plugin invocation so token writes to `common` precede component reads in `components`. If the plugin skill is not repo-graph aware, dispatch runs it against `repos.components` and performs the `common` token/key writes itself around the call.

## Failure + resume

On failure (the plugin skill returns an unrecoverable build/test error, or the dispatch hits retry cap):

1. The dispatch layer persists `{error, buildLog, testLog}` to `state.phases["2"].errors[]`, where the two log fields are paths rather than log text.
2. Multi-agent increments `state.phases["2"].retryCount` (the build-retry counter; review reworks are counted separately in `reworkCount`).
3. Retry re-invokes the plugin skill (it is idempotent on an existing component; it reconciles rather than duplicating). There is no bundled `phase-<N>` resume anymore.
4. Hard kill when `retryCount` reaches the build-retry cap (3, unrelated to the review limit) → surface the errors to the user, halt Phase 2. Do not loop indefinitely.

## Cross-CLI behaviour (intentional divergence)

Component dispatch is **not byte-identical across CLIs**, by design (see `cross-cli-contract.md` section 1.1):

- **Claude Code**: dispatches to the enabled `ai-<platform>-toolkit` marketplace plugin via the Skill tool (this doc).
- **Copilot CLI**: has no plugin loader; `install/copilot.mjs` copies the enabled stack plugin's skills into `~/.copilot/skills/`, and Copilot's resolution + progress lines follow those local copies.

What still MUST match across CLIs: the `taskType === "component"` classification and the `state.phases["2"].componentDispatch` record the dispatch layer writes. Figma-skill *inventory* parity is not enforced. `smoke-cross-cli-behavior.sh` asserts only the classification + state-shape axis for components.
