# Workflow Standard: Parallel Execution

## When to Use Parallel Execution

| Condition | Decision |
|-----------|----------|
| 5+ independent tasks | Use P-Threads |
| Multi-domain (backend + frontend + infra) | Use P-Threads with squad templates |
| 15+ files to create | Use P-Threads |
| Tasks have zero dependency overlap | Use P-Threads |
| Tasks share file scope | Do NOT parallelize |
| Sequential data flow required | Do NOT parallelize |

## Parallelization Planning

### Step 1: Dependency Analysis
```bash
morph-spec detect-workflow user-auth

# Identifies: parallelizable tasks, sequential chains, waves
```

### Step 2: File Scope Assignment
Assign exclusive file ownership to each parallel worker:

```
Worker A (Backend):    Entities/, Services/, Controllers/, DTOs/
Worker B (Frontend):   Pages/, Components/, CSS/
Worker C (Infra):      infra/bicep/, .github/workflows/
```

No overlap allowed. If overlap is unavoidable, make it sequential.

### Step 3: Choose Squad Template
```bash
morph-spec squad-template use full-stack --feature=user-auth

# Creates execution plan based on full-stack.json template:
# - dotnet-senior (backend lead)
# - blazor-builder (frontend lead)
# - bicep-architect (infra lead)
# - thread-orchestrator (coordinator)
```

## Execution Flow

```
Orchestrator
    │
    ├── spawn parallel: Worker A (files: Entities/, Services/)
    ├── spawn parallel: Worker B (files: Pages/, Components/)
    └── spawn parallel: Worker C (files: infra/)
    │
    │   [Workers execute concurrently — no interaction]
    │
    ├── wait-all (poll or explicit sync)
    │
    ├── merge results (check for conflicts)
    ├── run integration validation
    └── report consolidated output
```

## Conflict Resolution

### File Conflicts (two workers wrote same file)
```bash
# This should NOT happen if scopes are properly defined
# If it does:
morph-spec agents review aggregate user-auth \
  --agents=dotnet-senior,blazor-builder \
  --strategy=best-of-n
```

### Interface Mismatches (A expects contract B didn't fulfill)
- Document the mismatch in the coordinator report
- Fix in post-merge sequential step
- Update contracts.cs to reflect actual implementation

## Fusion vs Parallel

| Aspect | Parallel (P-Thread) | Fusion (F-Thread) |
|--------|--------------------|--------------------|
| Goal | Divide scope | Compete on same scope |
| Output | Different files | Same files, best one wins |
| Workers collaborate? | No | No |
| Merge | Combine all files | Select best implementation |
| Use case | Large features | Design decisions, quality optimization |

## Configuration
```json
// llm-interaction.json
{
  "parallelExecution": {
    "enabled": true,
    "maxConcurrent": 3,
    "defaultStrategy": "best-of-n"
  }
}
```

## Squad Templates

Pre-defined squad configurations in `framework/squad-templates/`:
- `full-stack.json` — Backend + Frontend + Infra (6 agents)
- `backend-only.json` — Backend specialists only (4 agents)
- `frontend-only.json` — Frontend specialists only (3 agents)

```bash
morph-spec squad-template list
morph-spec squad-template show full-stack
morph-spec squad-template use full-stack --feature=user-auth
```
