# Promote Bug to Task Master Task: $ARGUMENTS

## Phase 0: Parse Arguments

Extract bug identifier from `$ARGUMENTS`:

- **Bug number** (e.g., "2", "3")
- **Bug text** for fuzzy matching (e.g., "selective install")
- **Empty**: Process all non-closed bugs

## Phase 1: Read Bug File

### 1.1 Check for Bug File

```bash
test -f .bugs.local.md
```

**If .bugs.local.md NOT found:**

- Error: "⚠️ No bugs file found. Nothing to promote to tasks."
- Exit with status 0

### 1.2 Read and Parse Bugs

```bash
cat .bugs.local.md
```

Parse the summary table to extract:

- Issue ID (row number in table, starting from 1)
- Summary text
- Status
- Priority
- Task Reference (if already linked)

### 1.3 Filter Bugs

If `$ARGUMENTS` is empty:

- Select all bugs where Status != "Closed"

If `$ARGUMENTS` is a number:

- Select bug with matching ID
- Error if not found: "Bug #X not found. Use /bug:list to see available bugs."

If `$ARGUMENTS` is text:

- Fuzzy match against Summary field
- If multiple matches, show list and ask user to specify bug number
- If no matches: "No bugs match '{text}'. Use /bug:list to see available bugs."

## Phase 2: Get Task Master Tasks

For each selected bug, check if already covered by existing Task Master task:

```javascript
mcp__task_master_ai__get_tasks({
  projectRoot: '/absolute/path/to/project',
  withSubtasks: true,
});
```

### 2.1 Check for Existing Coverage

For each bug, search Task Master tasks for matches:

- Match by title (fuzzy matching on bug summary)
- Match by Task Reference field in bug (if populated)
- Match by description keywords

If match found:

- Skip this bug
- Add to "Already Covered" list with task ID

## Phase 3: Create Task Master Tasks

For each bug NOT already covered:

### 3.1 Prepare Task Data

Extract from bug details section:

- **Title**: Bug summary
- **Description**: From bug Description section
- **Priority**: Map bug priority to task priority:
  - Blocker → high
  - Critical → high
  - High → high
  - Medium → medium
  - Low → low
- **Details**: Combine:
  - Bug description
  - Triage notes
  - Repository context
  - Any other relevant sections

### 3.2 Create Task via MCP

```javascript
mcp__task_master_ai__add_task({
  projectRoot: '/absolute/path/to/project',
  title: bug.summary,
  description: bug.description,
  priority: mappedPriority,
  details: combinedDetails,
  research: true, // Enable research for informed task generation
});
```

### 3.3 Update Bug File

Add Task Master reference to bug's Task Reference field:

- Format: `TM:#<task-id>`
- Update the summary table row
- Append to existing references if any (e.g., "Jira:ABC-123, TM:#26")

Update bug status if currently "Open":

- Set to "Ready" (now has task for implementation)

Add triage note:

```text
- YYYY-MM-DD: Promoted to Task Master task #<task-id>
```

## Phase 4: Report Results

### 4.1 Summary Statistics

```text
📋 Bug to Task Promotion Summary

Processed: [count] bugs
Created: [count] new tasks
Already Covered: [count] bugs
Skipped: [count] bugs
```

### 4.2 Created Tasks List

For each newly created task:

```text
✓ Bug #[bug-id]: [summary]
  → Created Task Master task #[task-id]
  → Priority: [priority]
  → Subtasks: [count]
```

### 4.3 Already Covered List

For bugs already covered:

```text
ℹ Bug #[bug-id]: [summary]
  → Already covered by Task #[task-id]
```

### 4.4 Skipped Bugs

For bugs skipped (e.g., Closed status):

```text
⊘ Bug #[bug-id]: [summary]
  → Status: [status] (not eligible for promotion)
```

### 4.5 Next Steps

```text
Next Steps:
  /bug:list              # View updated bug list
  Get next task via Task Master MCP tools
  /bug:show <id>         # View bug details
```

## Error Handling

- **No bugs to process**: "No eligible bugs found for promotion."
- **Bug not found**: Show available bugs with /bug:list
- **Task Master not initialized**: "Task Master not initialized in this project. Initialize first."
- **Multiple fuzzy matches**: Show list, ask user to specify bug number
- **Task creation failed**: Show error, continue with remaining bugs

## Optimization Notes

- **Batch processing**: Create all tasks without user prompts
- **Parallel MCP calls**: If creating multiple tasks, make parallel calls where possible
- **Minimal output**: Show summary first, detailed list after
- **Auto-update bug file**: No confirmation needed for status/reference updates

## Exit Codes

- 0: Success (even if no bugs processed)
- 1: Bug file not found or unreadable
- 2: Task Master not available
- 3: Invalid bug identifier
