---
description: Clear bugs by removing them from the bug tracking file
argument-hint: [<bug-id>] [--all] [--force]
allowed-tools: Bash, Read, Edit, Write
---

# Clear Bug: $ARGUMENTS

Remove bugs from `.bugs.local.md` by deleting them from the summary table and detailed sections. By default, only clears bugs with status "Resolved" or "Closed".

## Phase 0: Parse Arguments

Extract from `$ARGUMENTS`:

- **bug-id**: Optional bug ID to clear (can be inferred from conversation if not provided)
- **--all**: Clear all eligible bugs (Resolved/Closed status)
- **--force**: Skip confirmation prompt

```bash
# Parse arguments
BUG_ID=""
CLEAR_ALL=false
FORCE=false

for arg in $ARGUMENTS; do
  case "$arg" in
    --all) CLEAR_ALL=true ;;
    --force) FORCE=true ;;
    --*) echo "❌ Unknown flag: $arg"; exit 1 ;;
    *) BUG_ID="$arg" ;;
  esac
done
```

### 0.1 Infer Bug ID from Conversation History

**If no arguments provided** (no bug-id and no --all flag):

Search recent conversation history for bug references:

1. Look for explicit bug ID mentions:
   - "Bug #42"
   - "#42"
   - "bug 42"
   - "/bug:show 42"
   - "/bug:triage 42"

2. Check for bug context from recent commands:
   - Previous /bug:show output
   - Previous /bug:list output
   - Previous /bug:triage interactions

3. Extract most recent bug ID from matches

**If bug ID found in conversation:**

```text
ℹ️  Inferred bug from conversation: Bug #[ID]

  Summary: [summary from file]
  Status:  [status]

Proceeding with Bug #[ID]...
```

Set `BUG_ID` to inferred value and continue to Phase 1.

**If no bug ID found in conversation:**

```text
❌ No bug ID specified and none found in recent conversation

Usage:
  /bug:clear <bug-id>         Clear specific bug
  /bug:clear --all            Clear all resolved/closed bugs
  /bug:clear <bug-id> --force Clear without confirmation

Examples:
  /bug:clear 42
  /bug:clear --all
  /bug:clear 42 --force

To see available bugs:
  /bug:list
```

Exit with status 1

### 0.2 Validate Arguments

**If both bug-id and --all provided:**

- Error: "❌ Cannot specify both bug ID and --all flag"
- Exit with status 1

## Phase 1: Read and Validate Bug File

### 1.1 Check File Exists

```bash
test -f .bugs.local.md || { echo "⚠️  No bugs found."; exit 0; }
```

### 1.2 Find Bugs to Clear

**If bug-id provided:**

Parse `.bugs.local.md` and locate bug by ID.

**If bug not found:**

```text
❌ Bug #[id] not found
Use /bug:list to see available bugs
```

Exit with status 1

**If --all flag:**

Find all bugs with status "Resolved" or "Closed":

```bash
# Parse table for Resolved/Closed bugs
grep -E '\| \[.*\]\(#.*\) \| (Resolved|Closed) \|' .bugs.local.md
```

**If no eligible bugs found:**

```text
⚠️  No Resolved or Closed bugs to clear

To clear bugs with other statuses, close them first:
  /bug:close <bug-id>

Or use specific bug ID:
  /bug:clear <bug-id> --force
```

Exit with status 0

## Phase 2: Validate Bug Status

**If clearing specific bug (not --all):**

Check bug status:

**If status is NOT "Resolved" or "Closed":**

```text
⚠️  Warning: Bug #[ID] is not resolved or closed

Current state:
  Status:     [status]
  Priority:   [priority]
  Resolution: [resolution]

Recommended workflow:
  1. Close bug first:  /bug:close [ID]
  2. Then clear:       /bug:clear [ID]

Or force clear:
  /bug:clear [ID] --force
```

**If --force not provided:**

Prompt for confirmation:

```text
? Clear bug #[ID] with status "[status]"? (y/N)
```

If N: Exit with status 0

## Phase 3: Confirm Deletion

**If --force flag provided:**

Skip confirmation, proceed to Phase 4

**Otherwise, show confirmation prompt:**

**For single bug:**

```text
? Clear bug #[ID] from .bugs.local.md? (y/N)

Bug details:
  Summary:    [summary]
  Status:     [status]
  Resolution: [resolution]
  Created:    [created-date]

⚠️  This will permanently remove the bug from the tracking file.
    Git history will preserve the record.
```

**For --all flag:**

```text
? Clear [count] resolved/closed bugs from .bugs.local.md? (y/N)

Bugs to clear:
  #[id1]: [summary1] (Closed)
  #[id2]: [summary2] (Resolved)
  #[id3]: [summary3] (Closed)
  [... up to 10, then "and X more"]

⚠️  This will permanently remove these bugs from the tracking file.
    Git history will preserve the records.
```

If N:

```text
✓ Cancelled - no bugs cleared
```

Exit with status 0

## Phase 4: Clear Bugs

### 4.1 Create Backup

```bash
# Always backup before modifying
cp .bugs.local.md .bugs.local.md.bak
```

### 4.2 Remove from Summary Table

For each bug to clear:

1. Find row in summary table by bug ID
2. Delete entire row
3. Maintain table structure (headers intact)

**Example removal:**

```markdown
Before:
| Issue | Status | TaskPriority | Resolution | Task Reference |
| ----- | ------ | ------------ | ---------- | -------------- |
| [Bug #1](#bug-1) | Closed | High | Fixed | Task Master: #26 |
| [Bug #2](#bug-2) | Open | Medium | Unresolved | |

After (clearing Bug #1):
| Issue | Status | TaskPriority | Resolution | Task Reference |
| ----- | ------ | ------------ | ---------- | -------------- |
| [Bug #2](#bug-2) | Open | Medium | Unresolved | |
```

### 4.3 Remove Detailed Section

For each bug to clear:

1. Find section by heading slug (e.g., `### Bug summary`)
2. Delete from section heading through end of section
3. Section ends at next `###` heading or end of file

**Section detection:**

```bash
# Find section by slug
SLUG="bug-summary-here"

# Extract section boundaries
# From: ### Bug summary
# To:   ### Next section (or EOF)
```

### 4.4 Renumber Remaining Bugs (Optional)

**Skip renumbering** - Keep original bug IDs for git history traceability

Gaps in bug numbering are acceptable and maintain historical references.

### 4.5 Atomic Write

```bash
# Write updated content to temp file
cat > .bugs.local.md.tmp << 'EOF'
[UPDATED CONTENT]
EOF

# Validate file structure
# - Check table syntax
# - Verify headers intact
# - Ensure markdown validity

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

# Remove backup on success
rm .bugs.local.md.bak
```

## Phase 5: Present Results

**For single bug:**

```text
✅ Bug #[ID] cleared from tracking

Removed:
  Summary:    [summary]
  Status:     [status]
  Resolution: [resolution]

File updated: .bugs.local.md

The bug record is preserved in git history:
  git log -p .bugs.local.md
```

**For multiple bugs (--all):**

```text
✅ Cleared [count] bugs from tracking

Removed:
  #[id1]: [summary1] (Closed)
  #[id2]: [summary2] (Resolved)
  #[id3]: [summary3] (Closed)

File updated: .bugs.local.md

Remaining bugs: [remaining-count]
  Open:        [open-count]
  In Progress: [in-progress-count]
  Resolved:    [resolved-count]

View remaining: /bug:list
```

## Phase 6: Cleanup Suggestions

**If tracking file is getting large:**

```text
💡 Tip: Archive old bugs to keep tracking file focused

Create archive file:
  1. Create .bugs.archive.md
  2. Move historical bugs there
  3. Keep recent/active bugs in .bugs.local.md

Or rely on git history:
  git log --all --full-history -- .bugs.local.md
```

## Error Handling

- **File not found**: Exit gracefully with info message
- **Bug not found**: Show available bugs with /bug:list
- **Invalid bug ID**: Error message with usage
- **File write failure**: Restore from backup, show error
- **Corrupted table**: Attempt to repair or fail with clear message
- **Permission denied**: Show error, suggest checking permissions

**On write failure:**

```bash
# Restore backup
if [ -f .bugs.local.md.bak ]; then
  mv .bugs.local.md.bak .bugs.local.md
  echo "❌ Clear failed - file restored from backup"
fi
```

## Exit Codes

- 0: Bug(s) cleared successfully (or cancelled by user)
- 1: Invalid arguments or bug not found
- 2: File operation failure
- 3: File corruption detected

## Examples

### Clear single resolved bug

```bash
/bug:clear 42
```

### Clear without confirmation

```bash
/bug:clear 42 --force
```

### Clear all resolved/closed bugs

```bash
/bug:clear --all
```

### Clear all without confirmation

```bash
/bug:clear --all --force
```

### Force clear bug with any status

```bash
/bug:clear 42 --force
# Clears bug #42 regardless of status
```
