---
description: Close a bug after verification
argument-hint: <bug-id> [--force]
allowed-tools: Bash, Read, Edit, Write
---

# Close Bug: $ARGUMENTS

## Phase 0: Parse Arguments

Extract from `$ARGUMENTS`:

- **bug-id**: Required bug ID to close
- **--force**: Skip validation checks and close immediately

**If no bug-id provided:**

- Error: "❌ Bug ID required. Usage: /bug:close <bug-id>"
- Exit with status 1

## Phase 1: Read and Validate Bug

### 1.1 Check File Exists

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

### 1.2 Find Bug

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
```text

Exit with status 1

### 1.3 Check Current Status

**If status = Closed:**

```text
ℹ️  Bug #[ID] is already closed

Details:
  Summary:    [summary]
  Resolution: [resolution]
  Closed:     [date]
```text

Exit with status 0 (not an error)

## Phase 2: Validation (unless --force)

### 2.1 Check Resolution Status

**If Resolution = "Unresolved" or "Under Investigation":**

```text
⚠️  Warning: Bug #[ID] is not marked as resolved

Current state:
  Status:     [status]
  Resolution: Unresolved

This bug should be resolved before closing.

Options:
  1. Resolve first:   /bug:triage [ID]
  2. Force close:     /bug:close [ID] --force
  3. Cancel:          Ctrl+C
```text

Prompt for confirmation:

```text
? Proceed with closing unresolved bug? (y/N)
```text

If N: Exit with status 0

### 2.2 Check Status Progression

**If status != "Resolved":**

```text
⚠️  Recommended workflow: Open → Ready → In Progress → Resolved → Closed

Current status: [status]

Best practice: Mark as Resolved first
  /bug:triage [ID]
```text

Prompt:

```text
? Close anyway? (y/N)
```text

### 2.3 Verify Fix (Interactive)

If not --force:

```text
? Has this bug been verified as fixed? (Y/n)
```text

If N:

```text
ℹ️  Verification recommended before closing

Test checklist:
  • [ ] Fix deployed to appropriate environment
  • [ ] Original issue no longer reproducible
  • [ ] No regressions introduced
  • [ ] Related tests passing

Come back when verified: /bug:close [ID]
```text

Exit with status 0

## Phase 3: Gather Closing Information

### 3.1 Set Resolution (if not set)

If Resolution = "Unresolved":

```text
? How was this bug resolved?
❯ Fixed
  Won't Fix
  Duplicate (of another bug)
  Invalid (not a real issue)
```text

If "Duplicate":

```text
? Duplicate of bug #:
```text

Note duplicate reference in resolution

If "Won't Fix":

```text
? Reason (optional):
```text

### 3.2 Add Closing Notes (Optional)

```text
? Add closing notes? (Y/n)
```text

If yes:

```text
? Closing notes: (Press Enter when done)
> [User types notes about fix verification, deployment, etc.]
```text

## Phase 4: Update Bug File

### 4.1 Update Summary Table

Update bug row:

- Status: → "Closed"
- Resolution: → [selected-resolution]

### 4.2 Update Detailed Section

Update detailed report:

- **Status**: Closed
- **Resolution**: [selected-resolution]
- **Updated**: [current-date]
- **Closed**: [current-date]

If closing notes provided:

```markdown
**Closing Notes:**
- [YYYY-MM-DD]: [notes text]
```text

If duplicate:

```markdown
**Resolution**: Duplicate of Bug #[other-id]
```text

### 4.3 Atomic Write

```bash
# Backup
cp .bugs.local.md .bugs.local.md.bak

# Write to temp
cat > .bugs.local.md.tmp

# Validate
# (check structure)

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

# Cleanup
rm .bugs.local.md.bak
```text

## Phase 5: Post-Close Actions

### 5.1 Check for Fix Branch

```bash
# Look for related fix branches
git branch --all | grep -iE "fix.*#?[bug-id]|bug.*#?[bug-id]" 2>/dev/null
```text

If found:

```text
? Fix branch detected: [branch-name]

  Branch cleanup options:
  1. Keep branch (default)
  2. Delete local branch
  3. Delete local + remote branch

? Delete fix branch? (keep/local/remote) [keep]
```text

If "local":

```bash
git branch -d [branch-name]
```text

If "remote":

```bash
git branch -d [branch-name]
git push origin --delete [branch-name]
```text

### 5.2 Update Task Master (if linked)

If bug has Task Master reference in Task Reference column:

```text
? Update linked Task Master task #[task-id]? (Y/n)
```text

If yes:

```bash
# Update Task Master task with bug closure info
# Using MCP if available
mcp__task_master_ai__update_subtask \
  --id=[task-id] \
  --prompt="Bug #[bug-id] closed: [resolution]"
```text

### 5.3 Archive Suggestion

```text
💡 Tip: Keep .bugs.local.md clean by archiving old bugs

To archive closed bugs:
  1. Move to .bugs.archive.md
  2. Or remove from file entirely (git history preserves them)
```text

## Phase 6: Present Results

```text
✅ Bug #[ID] closed successfully

Summary:
  Title:      [summary]
  Resolution: [resolution]
  Duration:   [creation-date] → [close-date] ([days] days)

Closing notes: [notes if provided]

[If branch deleted:]
✓ Fix branch deleted: [branch-name]

[If Task Master updated:]
✓ Task Master #[task-id] updated

Next steps:
  View closed bugs:  /bug:list --status=Closed
  View all bugs:     /bug:list
```text

## Error Handling

- **Bug not found**: Show available bugs
- **Already closed**: Info message, exit gracefully
- **File write failure**: Restore from backup
- **Git operations fail**: Continue, note in output
- **Task Master update fails**: Note failure, continue with bug closure

## Exit Codes

- 0: Bug closed successfully (or already closed)
- 1: Bug not found or invalid ID
- 2: Validation failed and user declined to proceed
- 3: File operation failure
````
