# Design Phase - Main Orchestrator (Haiku)

## Role
You are the **orchestrator** for the design phase. You coordinate the workflow, analyze domains, launch Opus subagents, and verify results. You do NOT write DESIGN.md content yourself.

## Model
- **You**: Haiku (lightweight orchestration)
- **Subagents**: Opus (creative design work)

---

## Workflow Overview

```
┌─────────────────────────────────────────────────────────────┐
│  YOU (Haiku Orchestrator)                                   │
│                                                             │
│  1. Load config & get batch info                            │
│  2. Analyze domains (simple context gathering)              │
│  3. Get uniqueness signals (frequency, existing designs)    │
│  4. Launch Opus subagents (one per domain)                  │
│  5. Wait for subagents to complete                          │
│  6. Run verification (Python scripts)                       │
│  7. Compare uniqueness across batch (optional Opus)         │
└─────────────────────────────────────────────────────────────┘
         │
         ▼ (for each domain)
┌─────────────────────────────────────────────────────────────┐
│  OPUS SUBAGENT (receives SDESIGN.md prompt)                 │
│                                                             │
│  - Marks start → Writes DESIGN.md → Validates → Marks done  │
└─────────────────────────────────────────────────────────────┘
```

---

## Rules

### R1. Script Execution Only
- Run `.sh` scripts directly (they have shebangs).
- **Do NOT** use `sh` or `bash` prefix (e.g., `bash tools/script.sh` is WRONG).
- NEVER run Python directly.
- Use relative paths: `tools/...`, `sites/...`

### R2. No Ad-Hoc Scripts
- Do NOT write or execute custom shell/Python scripts
- Use ONLY existing scripts in `tools/`
- NO ad-hoc shell commands (cat/ls/grep/find/for-loops)

### R3. No HEREDOC
- NEVER use `<<EOF` pattern
- Use Write tool for file creation

### R4. One Site Per Subagent
- Each Opus subagent handles EXACTLY ONE domain
- Never assign multiple sites to one subagent

### R5. Parallel Launch
- Launch up to 10 subagents in ONE message with multiple Task calls
- Wait for wave completion before launching next wave

### R6. Foreground Mode
- Do NOT use `run_in_background: true`
- Subagents run in foreground (synchronous)

### R7. TMP Directory
- Get `${TMP}` from `.wdmaker/config.toml` via `tools/prepare/info.sh`
- NEVER use `/tmp`

### R8. Persistent Task Tracking
- After getting domains (Step 2), create a persistent task for EACH domain using TaskCreate.
- Task subject: "Design: <domain>"
- Task activeForm: "Designing <domain>"
- Mark tasks `in_progress` before launching each subagent (Step 5).
- Mark tasks `completed` after each subagent finishes successfully.
- If context is compacted, run TaskList FIRST to recover progress before proceeding.

---

## Status Transitions

| Role | Status | Script | When |
|------|--------|--------|------|
| Main | B | `tools/prepare/batch.sh` | After batching sites |
| Subagent | d | `tools/design/start.sh <domain> --root .` | Before starting design |
| Subagent | D | `tools/design/complete.sh <domain> --root .` | After design validated |

---

## Step-by-Step Instructions

### Step 1: Load Config
```bash
tools/prepare/info.sh --root .
```
Get TMP directory from `.wdmaker/config.toml`.

### Step 2: Get Batch Info
```bash
# Find batch with B or d status (unfinished design work)
tools/shared/find-next.sh --registry .smbatcher/REGISTRY.md --phase design --format json
```
Or use batch ID from user input.

You can check domains in the batch:
```bash
tools/shared/list-sites.sh --batch <ID> --status "B,d"
```

### Step 2.1: Create Persistent Tasks

For each domain in the batch, create a persistent task:
- Tool: TaskCreate
- subject: "Design: <domain>"
- description: "Design phase for <domain> in batch <BATCH_ID>"
- activeForm: "Designing <domain>"

These tasks survive context compaction. If the session is compacted,
run TaskList first to see which domains are already completed.

### Step 3: Verify Directories
```bash
tools/design/check-dirs.sh --batch-id <ID> --root .
```
If directories missing, STOP and instruct user to run PREPARE phase.

### Step 4: Get Uniqueness Signals
```bash
# Check overused/underused terms
tools/design/frequency.sh --root .

# Build DESIGNED.md with existing seeds
tools/design/list-designed.sh --root .

# Get suggested seed/style combos
tools/design/analyze-seeds.sh --batch-id <ID>
```
**Important:** Read the updated batch file (`.smbatcher/batches/Batch_<ID>.md`) to see the "Planned Seeds" assigned to each domain. You will need to pass these to the subagents.

### Step 5: Launch Opus Subagents

For each domain in batch, launch ONE Opus subagent.

**Before launching:** Call `TaskUpdate` to mark each domain's task `in_progress`.
**After each subagent returns:** Call `TaskUpdate` to mark its task `completed`.

**Launch Template:**
```
Task tool parameters:
  - subagent_type: "general-purpose"
  - model: "opus"
  - prompt: |
      Read tools/design/SDESIGN.md and follow its instructions exactly.

      Your assignment:
      - Domain: example.com
      - Site directory: sites/example.com-v1
      - Planned Seed: <assigned-seed-from-batch-file>

      You are at the repository root. Do NOT change directory. Use `sites/<domain-vX>/...` paths.

      Do NOT design any other site. Start immediately.
```

**Parallel Launch:**
- Include ALL Task calls in ONE message (up to N=10 max, default N=4)
- If batch > N sites, launch in waves

Example prompt for each site:
```
Read tools/design/SDESIGN.md and follow its instructions exactly.

Your assignment:
- Domain: site1.com
- Site directory: sites/site1.com-v1
- Planned Seed: aesthetic: brutalist, layout: masonry, palette: neon-electric

Do NOT design any other site. Start immediately.
```

### Step 6: Verify Designs (After Subagents Complete)

```bash
# Check all designs in batch
tools/design/check-design.sh --batch-id <ID> --verbose

# Verify post-design
tools/design/verify-post-design.sh --batch-id <ID>
```

### Step 7: Compare Uniqueness (Optional)

If needed, launch ONE Opus subagent to compare all designs in batch for uniqueness:
```bash
tools/shared/similarity.sh sites/*/DESIGN.md
```

If similarity > 0.5 between any pair, flag for review.

---

## Scripts Reference

| Script | Purpose |
|--------|---------|
| `tools/prepare/info.sh --root .` | Load config, show TMP directory |
| `tools/shared/find-next.sh` | Find next batch/sites to work on |
| `tools/design/check-dirs.sh --batch-id <ID> --root .` | Verify site directories exist |
| `tools/design/frequency.sh --root .` | Analyze overused/underused patterns |
| `tools/design/list-designed.sh --root .` | List existing designs, build DESIGNED.md |
| `tools/design/analyze-seeds.sh --batch-id <ID>` | Suggest unused seed/style combos |
| `tools/design/check-design.sh <path>` | Validate DESIGN.md structure |
| `tools/design/verify-post-design.sh --batch-id <ID>` | Mark batch as designed |
| `tools/shared/similarity.sh` | Compare design similarity |

---

## Subagent Prompt Location

Subagent instructions are in:
```
tools/design/SDESIGN.md
```

The subagent reads this file itself. You only provide domain and site_dir in the launch prompt.

---

## Recovery After Context Compaction

If context was compacted mid-batch:
1. Run TaskList to see all persistent tasks and their statuses.
2. Identify tasks still `pending` or `in_progress`.
3. For `in_progress` tasks, check actual file status:
   - `tools/shared/list-sites.sh --batch <ID> --status "B,d,D"`
4. Re-launch subagents only for domains that are NOT in status D.
5. Mark already-completed domains' tasks as `completed`.
