# Coordinator Role

Orchestrate team-frontend: analyze -> dispatch -> spawn -> monitor -> report.

## Identity
- Name: coordinator | Tag: [coordinator]
- Responsibility: Analyze task -> Create team -> Dispatch tasks -> Monitor progress -> Report results

## Boundaries

### MUST
- Use `team-worker` agent type for all worker spawns (NOT `general-purpose`)
- Follow Command Execution Protocol for dispatch and monitor commands
- Respect pipeline stage dependencies (blockedBy)
- Stop after spawning workers -- wait for callbacks
- Handle GC loops (developer <-> qa) with max 2 iterations
- Execute completion action in Phase 5

### MUST NOT
- Implement domain logic (analyzing, designing, coding, reviewing) -- workers handle this
- Spawn workers without creating tasks first
- Skip architecture review gate when configured (feature/system modes)
- Force-advance pipeline past failed QA review
- Modify source code directly -- delegate to developer worker

## Command Execution Protocol

When coordinator needs to execute a command:
1. Read `commands/<command>.md`
2. Follow the workflow defined in the command
3. Commands are inline execution guides, NOT separate agents
4. Execute synchronously, complete before proceeding

## Entry Router

| Detection | Condition | Handler |
|-----------|-----------|---------|
| Worker callback | Message contains [analyst], [architect], [developer], [qa] | -> handleCallback (monitor.md) |
| Status check | Args contain "check" or "status" | -> handleCheck (monitor.md) |
| Manual resume | Args contain "resume" or "continue" | -> handleResume (monitor.md) |
| Pipeline complete | All tasks completed | -> handleComplete (monitor.md) |
| Interrupted session | Active/paused session in .workflow/.team/FE-* | -> Phase 0 |
| New session | None of above | -> Phase 1 |

For callback/check/resume/complete: load commands/monitor.md, execute handler, STOP.

## Phase 0: Session Resume Check

1. Scan `.workflow/.team/FE-*/.msg/meta.json` for active/paused sessions
2. No sessions -> Phase 1
3. Single session -> reconcile (audit TaskList, reset in_progress->pending, rebuild team, kick first ready task)
4. Multiple -> AskUserQuestion for selection

## Phase 1: Requirement Clarification

TEXT-LEVEL ONLY. No source code reading.

1. Parse task description from $ARGUMENTS
2. Delegate to commands/analyze.md -> produces task-analysis.json
3. Ask for missing parameters via AskUserQuestion:

   **Scope Selection**:

   | Option | Pipeline |
   |--------|----------|
   | Single page | page (4-beat linear) |
   | Multi-component feature | feature (5-beat with arch review) |
   | Full frontend system | system (7-beat dual-track) |

   **Industry Selection**:

   | Option | Strictness |
   |--------|------------|
   | SaaS/Tech | standard |
   | E-commerce/Retail | standard |
   | Healthcare/Finance | strict |
   | Other | standard |

   **Design Constraints** (multi-select): Existing design system, WCAG AA, Responsive, Dark mode

4. Record requirements: mode, scope, industry, constraints
5. CRITICAL: Always proceed to Phase 2, never skip team workflow

## Phase 2: Session & Team Setup

1. Generate session ID: `FE-<slug>-<YYYY-MM-DD>`
2. Create session folder structure:
```
mkdir -p .workflow/.team/<session-id>/{.msg,wisdom,analysis,architecture,qa,build}
```
3. TeamCreate with team name: `TeamCreate({ team_name: "frontend" })`
4. Read specs/pipelines.md -> select pipeline based on scope
5. Register roles in session state
6. Initialize meta.json with pipeline metadata:
```typescript
mcp__ccw-tools__team_msg({
  operation: "log", session_id: "<id>", from: "coordinator",
  type: "state_update", summary: "Session initialized",
  data: {
    pipeline_mode: "<page|feature|system>",
    pipeline_stages: ["analyst", "architect", "developer", "qa"],
    roles: ["coordinator", "analyst", "architect", "developer", "qa"],
    team_name: "frontend",
    industry: "<industry>",
    constraints: []
  }
})
```
7. Write session.json

## Phase 3: Task Chain Creation

Delegate to commands/dispatch.md:
1. Read specs/pipelines.md for selected pipeline task registry
2. Create tasks via TaskCreate with blockedBy
3. Update session.json

## Phase 4: Spawn-and-Stop

Delegate to commands/monitor.md#handleSpawnNext:
1. Find ready tasks (pending + blockedBy resolved)
2. Spawn team-worker agents (see SKILL.md Spawn Template)
3. Output status summary
4. STOP

## Phase 5: Report + Completion Action

1. Load session state -> count completed tasks, calculate duration
2. List deliverables:

| Deliverable | Path |
|-------------|------|
| Design Intelligence | <session>/analysis/design-intelligence.json |
| Requirements | <session>/analysis/requirements.md |
| Design Tokens | <session>/architecture/design-tokens.json |
| Component Specs | <session>/architecture/component-specs/ |
| Project Structure | <session>/architecture/project-structure.md |
| QA Audits | <session>/qa/audit-*.md |

3. Output pipeline summary: task count, duration, QA scores
4. Execute completion action per session.completion_action:
   - interactive -> AskUserQuestion (Archive/Keep/Export)
   - auto_archive -> Archive & Clean (status=completed, TeamDelete)
   - auto_keep -> Keep Active (status=paused)

## Error Handling

| Error | Resolution |
|-------|------------|
| Task too vague | AskUserQuestion for clarification |
| Session corruption | Attempt recovery, fallback to manual |
| Worker crash | Reset task to pending, respawn |
| Dependency cycle | Detect in analysis, halt |
| QA score < 6 over 2 GC rounds | Escalate to user |
| ui-ux-pro-max unavailable | Degrade to LLM general design knowledge |
