# Phase 01.2: Git Worktree Auto-Management [CONDITIONAL]

**Purpose:** Automatically create and manage Git worktrees for parallel story development (STORY-091).

**When to execute:** Only when `$GIT_AVAILABLE == true` AND worktree management is enabled in config.

**Pre-check: Configuration enabled flag:**

```
# Load parallel.yaml config
config_path = "devforgeai/config/parallel.yaml"

IF file_exists(config_path):
    config = load_yaml(config_path)
ELSE:
    config = {enabled: true}  # Default enabled

IF config.enabled == false:
    Display: "Worktree management disabled via config - using branch-only workflow"
    SKIP to Phase 01.3
    RETURN
```

**Invoke git-worktree-manager subagent:**

```
Task(
  subagent_type="git-worktree-manager",
  description="Manage worktree for ${STORY_ID}",
  prompt="Manage Git worktree for story ${STORY_ID}.

    Configuration: devforgeai/config/parallel.yaml

    Tasks:
    1. Load configuration (threshold, max, pattern)
    2. Check for existing worktree for this story
    3. Scan all worktrees for idle detection
    4. Validate integrity if worktree exists
    5. Determine required action

    Return JSON with status and actions."
)
```

**Parse subagent response and handle actions:**

```javascript
result = parse_json(subagent_output)

// Handle story-specific worktree
IF result.story_worktree.action_needed == "CREATE":
    path = result.story_worktree.path
    branch = result.story_worktree.branch
    Bash(command="git worktree add ${path} -b ${branch}", description="Create story worktree")
    Display: "Created worktree: ${path} (branch: ${branch})"
    $WORKTREE_PATH = path

ELIF result.story_worktree.action_needed == "RESUME":
    path = result.story_worktree.path
    Display: "Resuming in existing worktree: ${path}"
    $WORKTREE_PATH = path

ELIF result.story_worktree.action_needed == "REPAIR":
    // Corrupted worktree detected
    AskUserQuestion(
      questions=[{
        question: "Worktree at ${path} appears corrupted. How to proceed?",
        header: "Repair",
        options: [
          { label: "Delete and recreate", description: "Remove corrupted worktree, create fresh one" },
          { label: "Keep and continue", description: "Attempt to use existing (may have issues)" }
        ],
        multiSelect: false
      }]
    )

// Handle idle worktrees (if any detected)
IF result.idle_worktrees.length > 0:
    idle_count = result.idle_worktrees.length
    threshold = result.config.cleanup_threshold_days

    Display: "Found ${idle_count} idle worktrees (>${threshold} days):"
    FOR wt in result.idle_worktrees:
        Display: "  - ${wt.path} (idle ${wt.days_idle} days)"

    AskUserQuestion(
      questions=[{
        question: "How would you like to handle idle worktrees?",
        header: "Cleanup",
        options: [
          { label: "Resume Development", description: "Keep all worktrees, continue" },
          { label: "Fresh Start", description: "Delete current story worktree, create new" },
          { label: "Delete Old", description: "Delete idle worktrees not matching current story" }
        ],
        multiSelect: false
      }]
    )

// Handle max worktree limit
IF result.limit_reached AND result.story_worktree.action_needed == "CREATE":
    max = result.config.max_worktrees
    Display: "Maximum worktrees (${max}) reached."
    AskUserQuestion to select worktree to delete
```

**Token cost:** ~2,500 tokens (subagent call + response handling)

**References:**
- Subagent: `.claude/agents/git-worktree-manager.md`
- Configuration: `devforgeai/config/parallel.yaml`
