# Workflow Standard: Thread Management

## Thread Types

| Type | Code | Purpose | Lifecycle |
|------|------|---------|-----------|
| Base | B-Thread | Default implementation thread | Bounded by feature phase |
| Parallel | P-Thread | Concurrent isolated execution | Short (1 task scope) |
| Fusion | F-Thread | Best-of-N competing implementations | Short (1 prompt scope) |
| Long-Running | L-Thread | Multi-session persistent work | Days/weeks |
| Zero-Touch | Z-Thread | Fully autonomous execution | Bounded by trust level |

## Thread Lifecycle

```
pending → running → completed
                  → failed → (retry) → running
                           → killed
```

## Creating Threads
```bash
# Create a base thread (default)
morph-spec threads status morph-next-gen

# Create parallel threads for feature
morph-spec spawn-team morph-next-gen --parallel backend,frontend,infra

# Create fusion thread
morph-spec agents fuse \
  --prompt "Implement caching strategy for UserService" \
  --count=3 \
  --strategy=best-of-n \
  --feature=morph-next-gen
```

## Thread State in state.json
```json
{
  "threads": {
    "thread-abc12345": {
      "id": "thread-abc12345",
      "type": "parallel",
      "agent": "dotnet-senior",
      "feature": "user-auth",
      "status": "running",
      "startedAt": "2026-02-19T00:00:00Z",
      "events": [
        { "type": "started", "timestamp": "..." },
        { "type": "checkpoint_passed", "timestamp": "...", "checkpointNum": 1 }
      ],
      "metrics": {
        "tasksCompleted": 3,
        "checkpointsPassed": 1,
        "errors": 0
      }
    }
  }
}
```

## Parallel Execution Rules (P-Threads)

1. **File scope isolation**: Each P-Thread has exclusive write access to its files
2. **No cross-thread dependencies**: P-Thread tasks are self-contained
3. **Shared read access**: All P-Threads can read spec.md, standards, contracts
4. **Merge on completion**: Coordinator merges results after all P-Threads complete
5. **Max concurrent**: Default 3, max 5 (configurable in llm-interaction.json)

## Dependency Analysis
```bash
# Analyze task dependencies for parallel opportunities
morph-spec detect-workflow morph-next-gen

# Output:
# Parallelizable tasks: T001, T002, T003 (no dependencies)
# Sequential: T004 (depends on T001), T005 (depends on T002)
# Execution plan:
#   Wave 1: [T001, T002, T003] (parallel)
#   Wave 2: [T004, T005] (parallel, after wave 1)
#   Wave 3: [T006] (sequential)
```

## Long-Running Threads (L-Threads)

### Stop Hooks
L-Threads run periodic stop hooks every 30 minutes:
```json
// llm-interaction.json
{
  "stopHooks": {
    "enabled": true,
    "interval": 30,
    "maxRetries": 5,
    "hooks": ["validate-and-continue", "validate-checkpoints"]
  }
}
```

### Resume Pattern
```bash
# Resume L-Thread from bundle
morph-spec bundle load morph-next-gen
# Load resume context, continue from checkpoint
```

## Monitoring
```bash
morph-spec threads list morph-next-gen
morph-spec threads analytics morph-next-gen
morph-spec threads wait-all morph-next-gen
morph-spec threads wait-any morph-next-gen
```
