# Task: Smart Worktree Naming

> 🎯 **Auto-Detection** - Intelligently name worktrees based on work item type

## Description

Automatically detects the type of work item (story, bug, feature, investigation) and assigns appropriate worktree and branch naming conventions for optimal organization.

## Work Type Detection

### Pattern Recognition

```bash
# Auto-detect work type from filename and content
detect_work_type() {
  local file="$1"
  local basename=$(basename "$file")

  # Check filename patterns first
  case "$basename" in
    user-story-*)     echo "story" ;;
    story-*)          echo "story" ;;
    task-*)          echo "task" ;;
    bug-*)           echo "bug" ;;
    issue-*)         echo "bug" ;;
    defect-*)        echo "bug" ;;
    feature-*)       echo "feature" ;;
    enhancement-*)   echo "feature" ;;
    investigate-*)   echo "investigation" ;;
    analysis-*)      echo "investigation" ;;
    performance-*)   echo "investigation" ;;
    security-*)      echo "investigation" ;;
    *)
      # Check content patterns
      if grep -qi "bug\|defect\|error\|fix" "$file"; then
        echo "bug"
      elif grep -qi "investigation\|analysis\|research" "$file"; then
        echo "investigation"
      elif grep -qi "feature\|enhancement\|improvement" "$file"; then
        echo "feature"
      else
        echo "story"  # default
      fi
      ;;
  esac
}
```

## Naming Conventions

### Worktree Prefixes

| Work Type     | Prefix | Example                    |
| ------------- | ------ | -------------------------- |
| Task          | `tk-`  | `tk-auth`, `tk-cleanup`    |
| Story         | `st-`  | `st-auth`, `st-checkout`   |
| Bug           | `bg-`  | `bg-login`, `bg-payment`   |
| Feature       | `ft-`  | `ft-dashboard`, `ft-api`   |
| Investigation | `inv-` | `inv-perf`, `inv-security` |
| Spike         | `sp-`  | `sp-tech`, `sp-arch`       |
| Experiment    | `exp-` | `exp-ml`, `exp-cache`      |
| Hotfix        | `hf-`  | `hf-crit`, `hf-sec`        |
| Default       | `tk-`  | `tk-misc`, `tk-update`     |

### Branch Patterns

| Work Type     | Branch Prefix  | Example                                    |
| ------------- | -------------- | ------------------------------------------ |
| Story         | `story/`       | `story/auth`, `story/checkout`             |
| Bug           | `bug/`         | `bug/login`, `bug/payment`                 |
| Feature       | `feature/`     | `feature/dashboard`, `feature/api`         |
| Investigation | `investigate/` | `investigate/perf`, `investigate/security` |
| Default       | `task/`        | `task/misc`, `task/update`                 |

## Implementation

### Smart Naming Function

```bash
generate_worktree_name() {
  local file="$1"
  local work_type=$(detect_work_type "$file")
  local base_name=$(extract_short_name "$file")

  case "$work_type" in
    story)         echo "st-${base_name}" ;;
    bug)           echo "bg-${base_name}" ;;
    feature)       echo "ft-${base_name}" ;;
    investigation) echo "inv-${base_name}" ;;
    *)             echo "wt-${base_name}" ;;
  esac
}

generate_branch_name() {
  local file="$1"
  local work_type=$(detect_work_type "$file")
  local base_name=$(extract_short_name "$file")

  case "$work_type" in
    story)         echo "story/${base_name}" ;;
    bug)           echo "bug/${base_name}" ;;
    feature)       echo "feature/${base_name}" ;;
    investigation) echo "investigate/${base_name}" ;;
    *)             echo "task/${base_name}" ;;
  esac
}
```

### Short Name Extraction

```bash
extract_short_name() {
  local file="$1"
  local basename=$(basename "$file" .md)

  # Remove common prefixes and generate short name
  basename=${basename#user-story-}
  basename=${basename#bug-}
  basename=${basename#issue-}
  basename=${basename#feature-}
  basename=${basename#investigate-}

  # Convert to short form (max 8 chars, meaningful)
  case "$basename" in
    authentication*) echo "auth" ;;
    authorization*)  echo "authz" ;;
    dashboard*)      echo "dash" ;;
    payment*)        echo "pay" ;;
    checkout*)       echo "check" ;;
    performance*)    echo "perf" ;;
    security*)       echo "sec" ;;
    database*)       echo "db" ;;
    api*)           echo "api" ;;
    user-interface*) echo "ui" ;;
    *)              echo "${basename:0:8}" ;;  # truncate to 8 chars
  esac
}
```

## Example Usage

### Input Files and Generated Names

```bash
# Stories
user-story-authentication.md     → st-auth    (story/auth)
user-story-checkout-flow.md      → st-check   (story/check)
user-story-dashboard.md          → st-dash    (story/dash)

# Bugs
bug-login-failure.md             → bg-login   (bug/login)
issue-payment-timeout.md         → bg-pay     (bug/pay)
bug-ui-responsive.md             → bg-ui      (bug/ui)

# Features
feature-api-enhancement.md       → ft-api     (feature/api)
feature-dashboard-widgets.md     → ft-dash    (feature/dash)

# Investigations
investigate-performance.md       → inv-perf   (investigate/perf)
analysis-security-audit.md      → inv-sec    (investigate/sec)
performance-bottleneck.md       → inv-perf   (investigate/perf)
```

## Concurrent Execution Integration

### Agent Assignment with Smart Names

```json
{
  "agents": {
    "AGENT_A_AUTH": {
      "issue": "st-auth",
      "worktree": "st-auth",
      "branch": "story/auth",
      "type": "story",
      "focus": "Authentication implementation",
      "innovation_dimension": "error_handling"
    },
    "AGENT_B_LOGIN_BUG": {
      "issue": "bg-login",
      "worktree": "bg-login",
      "branch": "bug/login",
      "type": "bug",
      "focus": "Login failure investigation & fix",
      "innovation_dimension": "testing_strategies"
    },
    "AGENT_C_API_FEATURE": {
      "issue": "ft-api",
      "worktree": "ft-api",
      "branch": "feature/api",
      "type": "feature",
      "focus": "API enhancement implementation",
      "innovation_dimension": "security_hardening"
    },
    "AGENT_D_PERF_INVESTIGATION": {
      "issue": "inv-perf",
      "worktree": "inv-perf",
      "branch": "investigate/perf",
      "type": "investigation",
      "focus": "Performance bottleneck investigation",
      "innovation_dimension": "monitoring_approach"
    }
  }
}
```

## Benefits

### Organization Benefits

- **Clear Type Identification**: Instant recognition of work type from worktree name
- **Logical Grouping**: Related work items grouped by prefix
- **Consistent Naming**: Standardized across all parallel development

### Workflow Benefits

- **Quick Navigation**: `git worktree list` shows organized view
- **Branch Management**: Clear branch hierarchy by work type
- **Merge Strategy**: Type-specific merge and review processes

### Team Benefits

- **Reduced Confusion**: No ambiguity about worktree purpose
- **Better Coordination**: Clear work type helps with prioritization
- **Improved Tracking**: Type-based metrics and reporting

## Configuration

### Project-Level Configuration

```json
{
  "smartWorktreeNaming": true,
  "customPrefixes": {
    "hotfix": "hf-",
    "experiment": "exp-",
    "spike": "sp-"
  },
  "shortNameMappings": {
    "authentication": "auth",
    "authorization": "authz",
    "performance": "perf",
    "security": "sec"
  }
}
```

This smart naming system ensures that worktrees are immediately identifiable and properly organized, whether working on user stories, bug fixes, feature development, or technical investigations.
