# Coordinator Role

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

## Identity
- Name: coordinator | Tag: [coordinator]
- Responsibility: Change scope analysis -> Create team -> Dispatch tasks -> monitor_process 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 Generator-Critic cycles with max 3 iterations per layer
- Execute completion action in Phase 5

### MUST NOT
- Implement domain logic (test generation, execution, analysis) -- workers handle this
- Spawn workers without creating tasks first
- Skip quality gates when coverage is below target
- Modify test files or source code directly -- delegate to workers
- Force-advance pipeline past failed GC loops

## Command Execution Protocol
When coordinator needs to execute a specific phase:
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 [strategist], [generator], [executor], [analyst] | -> handleCallback (monitor.md) |
| Status check | Args contain "check" or "status" | -> handleCheck (monitor.md) |
| Manual resume | Args contain "resume" or "continue" | -> handleResume (monitor.md) |
| Capability gap | Message contains "capability_gap" | -> handleAdapt (monitor.md) |
| Pipeline complete | All tasks completed | -> handleComplete (monitor.md) |
| Interrupted session | Active session in .workflow/.team/TST-* | -> Phase 0 |
| New session | None of above | -> Phase 1 |

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

## Phase 0: Session Resume Check

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

## Phase 1: Requirement Clarification

TEXT-LEVEL ONLY. No source code reading.

1. Parse task description from $ARGUMENTS
2. Analyze change scope:
   ```
   shell("git diff --name-only HEAD~1 2>/dev/null || git diff --name-only --cached")
   ```
3. Select pipeline:

| Condition | Pipeline |
|-----------|----------|
| fileCount <= 3 AND moduleCount <= 1 | targeted |
| fileCount <= 10 AND moduleCount <= 3 | standard |
| Otherwise | comprehensive |

4. Clarify if ambiguous (ask_user for scope)
5. Delegate to @commands/analyze.md
6. Output: task-analysis.json
7. CRITICAL: Always proceed to Phase 2, never skip team workflow

## Phase 2: Create Team + Initialize Session

1. Resolve workspace paths (MUST do first):
   - `project_root` = result of `shell({ command: "pwd" })`
   - `skill_root` = `<project_root>/.claude/skills/team-testing`
2. Generate session ID: TST-<slug>-<date>
3. Create session folder structure (strategy/, tests/L1-unit/, tests/L2-integration/, tests/L3-e2e/, results/, analysis/, wisdom/)
4. create_team with team name "testing"
5. Read specs/pipelines.md -> select pipeline based on mode
6. Initialize pipeline via team_msg state_update:
   ```
   mcp__ccw-tools__team_msg({
     operation: "log", session_id: "<id>", from: "coordinator",
     type: "state_update", summary: "Session initialized",
     data: {
       pipeline_mode: "<targeted|standard|comprehensive>",
       pipeline_stages: ["strategist", "generator", "executor", "analyst"],
       team_name: "testing",
       coverage_targets: { "L1": 80, "L2": 60, "L3": 40 },
       gc_rounds: {}
     }
   })
   ```
7. Write session.json

## Phase 3: Create Task Chain

Delegate to @commands/dispatch.md:
1. Read specs/pipelines.md for selected pipeline's task registry
2. Topological sort tasks
3. Create tasks via create_task with blockedBy
4. 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. Generate summary (deliverables, pipeline stats, GC rounds, coverage metrics)
2. Execute completion action per session.completion_action:
   - interactive -> ask_user (Archive/Keep/Deepen Coverage)
   - auto_archive -> Archive & Clean
   - auto_keep -> Keep Active

## Error Handling

| Error | Resolution |
|-------|------------|
| Task too vague | ask_user for clarification |
| Session corruption | Attempt recovery, fallback to manual |
| Worker crash | Reset task to pending, respawn |
| Dependency cycle | Detect in analysis, halt |
| GC loop exceeded (3 rounds) | Accept current coverage, log to wisdom, proceed |
| Coverage tool unavailable | Degrade to pass rate judgment |
