# Issue Closure Pattern: Location & Implementation

**Date**: 2025-10-29 | **Reference**: context/ISSUE-CLOSURE-FIX.md

## Where the Pattern Is Defined

### 1. Primary Location: `/create-release-pr` Command

**File**: `commands/roadcrew/03-release/create-release-pr.md`

**What it does**: Creates release PR (dev → main) with automatic issue closure tracking

**Key function** (lines 95-106):
```bash
# Build the closure section for PR body
CLOSES_SECTION="## Issues Resolved\n\n"

for ISSUE in $(echo "$PENDING_ISSUES" | jq -r '.number'); do
  CLOSES_SECTION="$CLOSES_SECTION""Closes #$ISSUE\n"
done
```

**How it works**:
1. Finds all issues with `pending-closure` label
2. For each issue number, adds `Closes #N` to PR body
3. Builds the `$CLOSES_SECTION` variable
4. Includes in PR body when creating PR (lines 168-195)

### 2. autopilot.md References

**File**: `commands/roadcrew/04-implement/autopilot.md`

**References**:
- Line 638: "Create PR: dev → main with proper `Closes #N` keywords"
- Line 664: "Including `Closes #N` keywords for all pending issues"
- Line 765: "Automatically includes 'Closes #N' for all pending issues (epic + children)"

**Current behavior**:
- Tells users about the pattern but doesn't enforce it
- Says `/create-release-pr` handles it automatically
- Depends on `pending-closure` label being set

### 3. How the Pattern Works (Current Implementation)

```
Step 1: Epic PR (epic-360 → dev) merges
  ↓
Step 2: Add pending-closure labels to epic + child issues
  ↓
Step 3: Release PR (dev → main) uses /create-release-pr
  ↓
Step 4: /create-release-pr scans for pending-closure labels
  ↓
Step 5: For each found issue, adds "Closes #N" to PR body
  ↓
Step 6: PR merges → GitHub auto-closes all issues with "Closes #N"
```

## The Problem: Why It Failed for Epic #360

**The disconnect**: autopilot.md created PR #364 (epic-360 → dev) with INCORRECT body:

```
❌ WHAT AUTOPILOT DID:
┌─────────────────────────────────────┐
│ Closes #360                         │
│                                     │
│ (no mention of child issues)        │
└─────────────────────────────────────┘
```

**Why**:
1. autopilot.md creates the FIRST PR (epic → dev), not the release PR
2. autopilot.md doesn't implement the pattern, just references it
3. The pattern is implemented in `/create-release-pr` ONLY
4. `/create-release-pr` wasn't run to create the release PR initially
5. Instead, PR #365 was created manually or without the pattern

**When it should have worked**:
1. PR #364 should add `pending-closure` labels to #360 + children
2. Then `/create-release-pr` would scan those labels
3. Then `/create-release-pr` would generate the full `Closes` list

**What actually happened**:
- PR #365 was created but didn't follow the pattern
- Only "Closes #360" was included
- Child issues weren't in PR body
- GitHub only closed what was explicitly mentioned

## How to Fix: Update autopilot.md

### Current Code (Line 637-667)

The code is pseudocode/documentation, not executable. Currently it says:
```
- Run `/create-release-pr` command or create PR manually
- Create PR: dev → main with proper `Closes #N` keywords
```

### Required Fix

**Option A: Update autopilot.md to generate proper PR body**

Replace the PR creation code in autopilot.md (around line 630-660) with:

```markdown
### 5.2. AUTO-CREATE RELEASE PR (AUTOMATIC)

**This step is MANDATORY for projects with dev branch**

**Process:**
1. Fetch all child issues from epic
2. Generate Closes list for epic + ALL children
3. Create PR with comprehensive body

**Implementation:**

\`\`\`typescript
// Generate Closes list for all issues
const allIssues = [epicNumber, ...childIssues];
const closesList = allIssues
  .map(num => `Closes #${num}`)
  .join('\n');

// Example output:
// Closes #360
// Closes #336
// Closes #337
// ... etc

// Include in PR body:
const prBody = `
## Epic #${epicNumber}: [Title]

All issues implemented and tested.

### Closes

${closesList}

[rest of PR body...]
`;
\`\`\`
```

### Option B: Use /create-release-pr (Recommended)

Instead of creating PR manually in autopilot, invoke the existing command:

```markdown
**Automatically create the release PR:**

- Ensure all child issues have `pending-closure` label
- Run: `/create-release-pr`
- /create-release-pr will:
  * Scan for pending-closure labels (epic + children)
  * Generate comprehensive Closes list
  * Create PR with all auto-closures
```

## The Proper Flow (What Should Happen)

```
1. /autopilot 360 runs
   ├─ Analyzes epic
   ├─ Implements all issues
   ├─ Creates PR #364: epic-360 → dev
   └─ Adds pending-closure labels to epic + children
      (THIS IS KEY - must happen after epic PR creation)

2. PR #364 merges to dev
   └─ Issues now have pending-closure labels ✓

3. User runs /create-release-pr
   ├─ Scans for pending-closure label
   ├─ Finds: #360, #336, #337, #338, #339, #340
   ├─ Generates: Closes #360 \n Closes #336 \n ... etc
   └─ Creates PR #365 with full Closes list

4. PR #365 merges to main
   ├─ GitHub sees "Closes #360" → closes #360 ✓
   ├─ GitHub sees "Closes #336" → closes #336 ✓
   ├─ GitHub sees "Closes #337" → closes #337 ✓
   ├─ GitHub sees "Closes #338" → closes #338 ✓
   ├─ GitHub sees "Closes #339" → closes #339 ✓
   └─ GitHub sees "Closes #340" → closes #340 ✓

RESULT: All 6 issues closed ✅
```

## Missing Piece: pending-closure Label Assignment

**THIS WAS NOT DONE for Epic #360:**

autopilot.md should add this after creating the epic PR:

```markdown
### 4. ADD PENDING-CLOSURE LABELS (CRITICAL)

After epic PR merges to dev:

\`\`\`bash
# Add pending-closure label to epic
gh issue edit #${EPIC_NUMBER} --add-label pending-closure

# Add pending-closure label to ALL child issues
for ISSUE in "${CHILD_ISSUES[@]}"; do
  gh issue edit #${ISSUE} --add-label pending-closure
done
\`\`\`

**Why**: /create-release-pr scans for this label
```

## Files That Need Updates

### 1. commands/roadcrew/04-implement/autopilot.md (PRIORITY: HIGH)

**Current**: Creates PR but doesn't ensure labels or proper Closes list
**Update**: Add step to create pending-closure labels for ALL issues

**Location**: After PR creation (around line 634-660)

**Change**:
```
FROM:
  Create PR with epic-360 branch
  Add pending-closure labels to epic

TO:
  Create PR with epic-360 branch
  Add pending-closure labels to EPIC + ALL CHILD ISSUES
  Include note about /create-release-pr
```

### 2. commands/roadcrew/03-release/create-release-pr.md (REFERENCE)

**Current**: Already correct - scans for pending-closure and generates Closes list
**Status**: No changes needed - this is the reference implementation ✓

## Testing the Fix

### Before Phase 2, Verify:

1. Run `/autopilot 366` or `/autopilot 367`
2. Check that **BOTH** epic AND all child issues get `pending-closure` label
3. When ready to release, run `/create-release-pr`
4. Verify PR body has:
   ```
   Closes #366
   Closes #368
   Closes #369
   Closes #370
   Closes #371
   Closes #372
   ```
5. Merge PR
6. Verify ALL issues close (not just epic)

## Root Cause Summary

| Component | Current | Problem | Fix |
|-----------|---------|---------|-----|
| autopilot.md | Creates epic PR | Doesn't add labels to children | Add child issue label loop |
| create-release-pr.md | Generates Closes list | Wasn't run for Phase 1 | Use it or integrate into autopilot |
| Issue closure | Only epic closed | Missing child issue Closes | Include all in PR body |

## Action Items

**Immediate** (Before Phase 2):
- [ ] Review autopilot.md lines 630-667
- [ ] Add pending-closure label for ALL child issues (not just epic)
- [ ] Test: `/autopilot 366` should label both epic and children

**Medium-term** (Next sprint):
- [ ] Consider merging `/create-release-pr` logic into autopilot
- [ ] Or: ensure /create-release-pr is always called (not manual PR)

**Long-term** (Architecture):
- [ ] GitHub Actions workflow for automatic label management
- [ ] Validation webhook to prevent PRs without proper Closes list
- [ ] Pre-merge checks to verify all issues are closed

## References

- **Main implementation**: `commands/roadcrew/03-release/create-release-pr.md` (lines 95-106, 168-195)
- **autopilot PR creation**: `commands/roadcrew/04-implement/autopilot.md` (lines 630-670)
- **Issue #360 gap**: autopilot didn't add labels for child issues
- **Solution document**: context/ISSUE-CLOSURE-FIX.md

---

**Status**: Pattern identified, fix documented, ready to implement  
**Priority**: HIGH (affects all Phase 2 epics)
**Next**: Update autopilot.md BEFORE Phase 2 work begins
