---
description: Create a new bug in .bugs.local.md with repository context
argument-hint: <summary|file-path>
allowed-tools: Bash, Read, Edit, Write
---

# Create Bug: $ARGUMENTS

## Phase 0: Parse Arguments

Extract from `$ARGUMENTS`:

- **Input**: Required bug summary OR file path with bug details

**If no input provided:**

- Error: "❌ Bug input is required. Usage: /bug <summary|file-path>"
- Exit with status 1

### 0.1 Detect Input Type

Check if argument is a file path:

```bash
# Check if argument is a readable file
if [ -f "$ARGUMENTS" ]; then
  INPUT_TYPE="file"
else
  INPUT_TYPE="summary"
fi
```

### 0.2 Process File Input (if INPUT_TYPE=file)

Read file and extract bug information:

**Expected file format (flexible markdown):**

```markdown
# Bug Summary

One-line summary of the bug

## Description

Detailed description of the issue

## Steps to Reproduce

1. Step one
2. Step two

## Expected Behavior

What should happen

## Actual Behavior

What actually happens

## Additional Context

- Logs, screenshots, etc.
- Environment details
- Related issues
```

**File parsing logic:**

- Extract first H1 heading as summary (required)
- Extract Description section content
- Extract all other sections as additional details
- If file structure is invalid, show error and exit

**If file cannot be parsed:**

- Error: "❌ Invalid bug file format. Expected markdown with at least a heading."
- Exit with status 1

### 0.3 Process Summary Input (if INPUT_TYPE=summary)

Use `$ARGUMENTS` directly as bug summary (current behavior).

## Phase 1: Read or Create Bug File

### 1.1 Check for Existing Bug File

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

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

- Create from template (see Phase 1.2)
- Set nextBugId to 1

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

- Read file contents
- Parse current nextBugId from metadata or table
- Validate file structure

### 1.2 Create Template (if needed)

````markdown
# Issue Triage

| Issue | Status | TaskPriority | Resolution | Task Reference |
| ----- | ------ | ------------ | ---------- | -------------- |

> Note: update the summary below to reflect the issue details during triage and remediation.

<details>
  <summary>Status types explained</summary>

- Open: Issue is reported and not assessed yet.
- Rejected: Issue is not valid or not reproducible.
- Ready: Issue is confirmed and ready for development.
- In Progress: Issue is being worked on.
- Resolved: Issue is fixed and changes are merged.
- Closed: Issue is verified as fixed in production.

</details>

<details>
  <summary>Priority levels explained</summary>

- Low: Minor issue, no significant impact on functionality.
- Medium: Noticeable issue, but does not block major functionality.
- High: Major issue that affects core functionality and needs prompt attention.
- Critical: Severe issue that blocks all functionality and requires immediate resolution.
- Blocker: Issue that prevents any progress on the project and must be resolved before anything else.

</details>

<details>
  <summary>Resolution types explained</summary>

- Unresolved: Issue is still open and not yet addressed.
- Fixed: Issue has been resolved and changes are implemented.
- Won't Fix: Issue is acknowledged but will not be addressed.
- Duplicate: Issue is a duplicate of another reported issue.
- Invalid: Issue is not valid or does not pertain to the project.
- Under Investigation: Issue is being investigated and root cause is not yet determined.

</details>

<details>
  <summary>Task references explained</summary>

- Task ID: Reference to the task or ticket in the project management system that tracks the work for this issue, if applicable. Reference to the internal `task-master-ai` task ID if applicable.

Examples:

```markdown
| [Selective tool install not working](#selective-install-not-working) | Open | High | Under Investigation | [Jira](https://myorg.atlassian.net/browse/PROJECTID123-123), Task Master: `#26` |
```

</details>

## Issue Reports
````

## Phase 2: Gather Repository Context

### 2.1 Detect Git Information

```bash
# Get current branch
git branch --show-current 2>/dev/null || echo "unknown"

# Get remote URL
git config --get remote.origin.url 2>/dev/null || echo "none"

# Get current commit hash
git rev-parse --short HEAD 2>/dev/null || echo "none"
```

### 2.2 Detect Project Type

```bash
# Check for package.json (Node.js)
test -f package.json && echo "Node.js/TypeScript"

# Check for pyproject.toml or setup.py (Python)
test -f pyproject.toml || test -f setup.py && echo "Python"

# Check for Cargo.toml (Rust)
test -f Cargo.toml && echo "Rust"

# Check for go.mod (Go)
test -f go.mod && echo "Go"
```

### 2.3 Detect Framework (if applicable)

```bash
# Node.js projects
if [ -f package.json ]; then
  grep -E "react|vue|angular|next|nuxt|svelte" package.json
fi
```

## Phase 3: Generate Bug ID and Slug

### 3.1 Calculate Next Bug ID

- Parse existing table for highest bug number
- Increment by 1
- Format as simple number (e.g., "1", "2", "3")

### 3.2 Create URL Slug

```bash
# Convert summary to slug
# "Auth fails on Safari" → "auth-fails-on-safari"
echo "$SUMMARY" | tr '[:upper:]' '[:lower:]' | sed 's/[^a-z0-9]/-/g' | sed 's/--*/-/g' | sed 's/^-//' | sed 's/-$//'
```

## Phase 4: Add Bug to File

### 4.1 Add Table Row

Insert new row in summary table after header:

```markdown
| [Bug summary](#slug) | Open | Medium | Unresolved | |
```

Sort table rows by bug ID (newest first or last - maintain consistency with existing entries).

### 4.2 Add Detailed Report

Append to "## Issue Reports" section:

**If INPUT_TYPE=summary:**

```markdown
### Bug summary

**Status**: Open
**Priority**: Medium
**Resolution**: Unresolved
**Created**: YYYY-MM-DD
**Updated**: YYYY-MM-DD

**Repository Context:**

- Branch: [branch-name]
- Project: [project-type]
- Framework: [framework if detected]
- Commit: [short-hash]

**Description:**

[Placeholder for detailed description - to be filled during triage]

<details>
  <summary>Details</summary>

[Placeholder for technical details, logs, reproduction steps]

</details>
```

**If INPUT_TYPE=file:**

```markdown
### Bug summary

**Status**: Open
**Priority**: Medium
**Resolution**: Unresolved
**Created**: YYYY-MM-DD
**Updated**: YYYY-MM-DD

**Repository Context:**

- Branch: [branch-name]
- Project: [project-type]
- Framework: [framework if detected]
- Commit: [short-hash]

**Description:**

[Content from Description section of file]

<details>
  <summary>Details</summary>

[All other sections from file: Steps to Reproduce, Expected Behavior, Actual Behavior, Additional Context, etc.]

</details>
```

### 4.3 Write File Atomically

```bash
# Write to temp file
cat > .bugs.local.md.tmp

# Validate file structure
# Check table syntax, required headers, etc.

# Atomic rename
mv .bugs.local.md.tmp .bugs.local.md
```

## Phase 5: Present Results

Output to console:

**If INPUT_TYPE=summary:**

```text
✓ Bug #[ID] created: [Summary]
  Status: Open | Priority: Medium | Resolution: Unresolved

  Edit details: .bugs.local.md#[slug]

  Next steps:
  - /bug:show [ID]     # View full details
  - /bug:triage [ID]   # Update status/priority
  - /bug:fix [ID]      # Start working on fix
```

**If INPUT_TYPE=file:**

```text
✓ Bug #[ID] created from file: [Summary]
  Status: Open | Priority: Medium | Resolution: Unresolved
  Source: [file-path]

  Edit details: .bugs.local.md#[slug]

  Next steps:
  - /bug:show [ID]     # View full details
  - /bug:triage [ID]   # Update status/priority
  - /bug:fix [ID]      # Start working on fix
```

Open file in editor:

```bash
code .bugs.local.md
```

## Error Handling

- **Input missing**: Show usage and exit
- **File not found**: Error with "File not found: [path]"
- **Invalid file format**: Error with "Invalid bug file format. Expected markdown with at least a heading."
- **File read permission denied**: Error with "Cannot read file: [path]"
- **File corruption (.bugs.local.md)**: Create backup (.bugs.local.md.backup), create fresh template
- **Git not available**: Continue without git context, note in output
- **Write permission denied**: Show error, suggest checking file permissions
- **Invalid table structure**: Attempt to repair or fail with clear message

## Exit Codes

- 0: Bug created successfully
- 1: Invalid arguments or input
- 2: File operation failure
- 3: Corruption detected and could not repair
