# System Router

> Routes incoming tasks to appropriate workflows based on task type.
> Determines which phases, gates, and loops apply to each task.

---

## Task Classification

When Claude receives a task, it MUST classify it before proceeding.

### Classification Process

```
INPUT: User request
    │
    ▼
┌─────────────────────────────────┐
│ 1. Parse request for keywords   │
│ 2. Identify task indicators     │
│ 3. Match to task type           │
│ 4. Load appropriate workflow    │
└─────────────────────────────────┘
    │
    ▼
OUTPUT: task_type + workflow
```

---

## Task Types

### `feature` - New Feature Implementation

**Indicators:**
- "add", "create", "implement", "build", "new"
- "feature", "functionality", "capability"
- Adding new files, components, pages, APIs

**Workflow:**
```
ANALYZE → PLAN+SUGGEST → CONFIRM → IMPLEMENT → VERIFY → AUDIT → COMMIT
```

**Required Gates:**
- `plan_approval_gate`
- `verify_gate`
- `audit_gate` (UI enforcement)
- `pre_commit_gate`

**Suggestions:** Required

---

### `fix` - Bug Fix

**Indicators:**
- "fix", "bug", "broken", "doesn't work", "error"
- "issue", "problem", "wrong"
- Correcting existing behavior

**Workflow:**
```
ANALYZE → PLAN → CONFIRM → IMPLEMENT → VERIFY → COMMIT
```

**Required Gates:**
- `plan_approval_gate`
- `verify_gate`
- `pre_commit_gate`

**Suggestions:** Only if fix reveals missing handling

---

### `refactor` - Code Refactoring

**Indicators:**
- "refactor", "clean up", "restructure", "reorganize"
- "extract", "split", "merge", "rename"
- Improving code without changing behavior

**Workflow:**
```
ANALYZE → PLAN → CONFIRM → IMPLEMENT → VERIFY → AUDIT → COMMIT
```

**Required Gates:**
- `plan_approval_gate`
- `verify_gate`
- `audit_gate` (check for cycles)
- `pre_commit_gate`

**Suggestions:** Only if scope expands

---

### `style` - Styling/UI Changes

**Indicators:**
- "style", "css", "color", "layout", "design"
- "spacing", "font", "responsive", "mobile"
- Visual changes only

**Workflow:**
```
ANALYZE → PLAN → CONFIRM → IMPLEMENT → VERIFY → COMMIT
```

**Required Gates:**
- `plan_approval_gate`
- `verify_gate`
- `pre_commit_gate`

**Suggestions:** Skip

---

### `docs` - Documentation

**Indicators:**
- "document", "readme", "comment", "jsdoc"
- "explain", "describe"
- Documentation changes only

**Workflow:**
```
ANALYZE → PLAN → CONFIRM → IMPLEMENT → VERIFY → COMMIT
```

**Required Gates:**
- `plan_approval_gate`
- `verify_gate`
- `pre_commit_gate`

**Suggestions:** Skip

---

### `test` - Test Implementation

**Indicators:**
- "test", "spec", "coverage", "unit test"
- "integration test", "e2e"
- Adding or modifying tests

**Workflow:**
```
ANALYZE → PLAN → CONFIRM → IMPLEMENT → VERIFY → RUN_TESTS → COMMIT
```

**Required Gates:**
- `plan_approval_gate`
- `verify_gate`
- `test_gate` (tests must pass)
- `pre_commit_gate`

**Suggestions:** Only for test completeness

---

### `config` - Configuration Changes

**Indicators:**
- "config", "setting", "environment", "env"
- "setup", "install", "dependency"
- Configuration file changes

**Workflow:**
```
ANALYZE → PLAN → CONFIRM → IMPLEMENT → VERIFY → COMMIT
```

**Required Gates:**
- `plan_approval_gate`
- `verify_gate`
- `pre_commit_gate`

**Suggestions:** Skip

---

### `perf` - Performance Optimization

**Indicators:**
- "performance", "optimize", "speed", "fast"
- "slow", "lag", "memory", "bundle size"
- Performance improvements

**Workflow:**
```
ANALYZE → PLAN+SUGGEST → CONFIRM → IMPLEMENT → VERIFY → AUDIT → COMMIT
```

**Required Gates:**
- `plan_approval_gate`
- `verify_gate`
- `audit_gate`
- `pre_commit_gate`

**Suggestions:** Required (perf trade-offs)

---

### `security` - Security Updates

**Indicators:**
- "security", "vulnerability", "auth", "permission"
- "sanitize", "validate", "escape", "encrypt"
- Security-related changes

**Workflow:**
```
ANALYZE → PLAN → CONFIRM → IMPLEMENT → VERIFY → SECURITY_REVIEW → COMMIT
```

**Required Gates:**
- `plan_approval_gate`
- `verify_gate`
- `security_gate`
- `pre_commit_gate`

**Suggestions:** Required (security considerations)

---

### `query` - Information Request

**Indicators:**
- "what", "how", "why", "where", "explain"
- "show me", "find", "search", "list"
- Questions, not modifications

**Workflow:**
```
ANALYZE → RESPOND
```

**Required Gates:** None

**Suggestions:** Skip

---

## Routing Decision Tree

```
Is this a question/query?
├── YES → task_type: query
│         workflow: ANALYZE → RESPOND
│
└── NO → Does it add new functionality?
         ├── YES → task_type: feature
         │         workflow: full with AUDIT
         │
         └── NO → Does it fix a bug?
                  ├── YES → task_type: fix
                  │         workflow: standard
                  │
                  └── NO → Does it restructure code?
                           ├── YES → task_type: refactor
                           │         workflow: with AUDIT
                           │
                           └── NO → Is it visual only?
                                    ├── YES → task_type: style
                                    │         workflow: simple
                                    │
                                    └── NO → [continue classification]
```

---

## Workflow Definitions

### Full Workflow (Features)
```
┌─────────┐  ┌──────┐  ┌─────────┐  ┌───────────┐
│ ANALYZE │→ │ PLAN │→ │ CONFIRM │→ │ IMPLEMENT │
└─────────┘  └──────┘  └─────────┘  └───────────┘
                                          │
                                          ▼
┌────────┐  ┌───────┐  ┌────────┐  ┌──────────┐
│ COMMIT │← │ AUDIT │← │ VERIFY │← │   FIX    │
└────────┘  └───────┘  └────────┘  └──────────┘
                            ↑          │
                            └──────────┘ (if errors)
```

### Standard Workflow (Fixes, Styles, Docs)
```
┌─────────┐  ┌──────┐  ┌─────────┐  ┌───────────┐
│ ANALYZE │→ │ PLAN │→ │ CONFIRM │→ │ IMPLEMENT │
└─────────┘  └──────┘  └─────────┘  └───────────┘
                                          │
                                          ▼
              ┌────────┐  ┌────────┐  ┌──────────┐
              │ COMMIT │← │ VERIFY │← │   FIX    │
              └────────┘  └────────┘  └──────────┘
                              ↑          │
                              └──────────┘ (if errors)
```

### Query Workflow
```
┌─────────┐  ┌─────────┐
│ ANALYZE │→ │ RESPOND │
└─────────┘  └─────────┘
```

---

## Phase Definitions

| Phase | Purpose | Output |
|-------|---------|--------|
| ANALYZE | Understand codebase and requirements | Findings summary |
| PLAN | Design implementation approach | Proposed changes |
| SUGGEST | Offer completeness improvements | Categorized suggestions |
| CONFIRM | Get user approval | Approval or revision |
| IMPLEMENT | Write/modify code | Changed files |
| VERIFY | Run typecheck + lint | Pass/fail status |
| FIX | Correct errors | Fixed code |
| AUDIT | Check UI enforcement + cycles | Pass/fail status |
| COMMIT | Create git commit | Commit hash |
| RESPOND | Answer query | Information |

---

## Routing Output Format

After classifying a task, Claude MUST report:

```
## Task Classification

**Request:** "[user's request]"
**Type:** feature | fix | refactor | style | docs | test | config | perf | security | query
**Workflow:** ANALYZE → PLAN → CONFIRM → IMPLEMENT → VERIFY → [AUDIT] → COMMIT

**Required Gates:**
- [ ] plan_approval_gate
- [ ] verify_gate
- [ ] audit_gate (if applicable)
- [ ] pre_commit_gate

**Suggestions:** Required | Conditional | Skip

Proceeding with ANALYZE phase...
```

---

## Multi-Task Handling

If user provides multiple tasks:

1. **Classify each task separately**
2. **Present all classifications to user**
3. **Ask: "Should I handle these in order, or would you like to prioritize?"**
4. **Process one task at a time** (per rules)
5. **Complete each task fully before starting next**

```
## Multiple Tasks Detected

1. [Task 1] → type: feature
2. [Task 2] → type: fix
3. [Task 3] → type: style

Recommended order: 2, 1, 3 (fix first, then feature, then style)

Should I proceed in this order?
```

---

## Unknown Task Type

If task cannot be classified:

```
## Task Classification

**Request:** "[user's request]"
**Type:** UNCLEAR

I need clarification to proceed correctly:

Are you asking me to:
1. Add new functionality (feature)
2. Fix something broken (fix)
3. Improve existing code (refactor)
4. Just answer a question (query)

Please clarify so I can follow the appropriate workflow.
```

---

## Quick Reference

| Task Type | Workflow | Audit? | Suggestions? |
|-----------|----------|--------|--------------|
| feature | Full | Yes | Required |
| fix | Standard | No | Conditional |
| refactor | Full | Yes | Conditional |
| style | Simple | No | Skip |
| docs | Simple | No | Skip |
| test | Standard+ | No | Conditional |
| config | Simple | No | Skip |
| perf | Full | Yes | Required |
| security | Full+ | No | Required |
| query | Respond | No | Skip |

**Always classify before proceeding. Workflow depends on task type.**
