# Phase 0 Walkthrough: Per-Project Input and Worktree Setup (Copilot CLI)

Covers input parsing per project type and worktree creation only. It is not the
Phase 0 contract: the 8 sequential interactive steps live in
`$HOME/.claude/multi-agent-refs/phases/phase-0-init.md` and every one of them runs.

## Step 1  -  Project detection + input parsing

### my-ios-app (Jira + branch)
1. Input: `"PROJ-12345" "feature/PROJ-12345-order-filter"`
2. Parse directly: `jiraId = PROJ-12345`, `branch = feature/PROJ-12345-order-filter`
3. No GitHub interaction needed

### my-figma-app (GitHub Issue URL)
1. Input: `"https://github.com/my-org/my-figma-app/issues/316"`
2. Parse issue via `gh issue view 316 --json title,body,labels,assignees`
3. Extract Jira ID from issue title or body (pattern: `PROJ-XXXXX`)
4. Auto-generate branch: `feature/PROJ-{jiraId}-GH{issueNo}-{component-kebab}`
5. Assign issue to self: `gh issue edit 316 --add-assignee @me`
6. Move to In Progress: `gh issue edit 316 --add-label "in progress"`

### generic (any iOS project)
1. Accept multiple input formats:
   - **Free-text**: `"In LoginView the button color is invisible in dark mode, insufficient contrast"`
   - **GitHub Issue URL**: parse via `gh issue view`
   - **Jira ID + branch**: parse directly
2. If free-text:
   - Extract task summary from description
   - Auto-generate branch: `fix/{short-kebab-description}` or `feature/{short-kebab-description}`
   - Determine type from context: bug description → `fix/`, feature request → `feature/`
3. No Jira integration needed  -  task ID is enough for tracking

## Step 2  -  Worktree setup (all projects)

1. **Fetch latest from remote**: `git fetch origin`
2. Determine worktree path:
   - With a Jira ID: `.worktrees/PROJ-{id}/`
   - Without a Jira ID (generic): `.worktrees/task-{shortId}/` (e.g. `.worktrees/task-3/`)
3. Create or switch to git worktree:
   - Residue guard first (idempotent; keeps `.worktrees/` gitlinks out of `git add -A` in the parent tree): `ex="$(git rev-parse --path-format=absolute --git-common-dir)/info/exclude"; grep -qxF '.worktrees/' "$ex" 2>/dev/null || printf '.worktrees/\n' >> "$ex"`
   - If remote branch exists: `git worktree add {worktree-path} origin/{branch} -b {branch}`
   - If new branch: `git worktree add {worktree-path} -b {branch}`
   - If worktree already exists: `cd {worktree-path} && git pull origin {branch}`
4. Set git author (resolved from `prefs.global.identities[]`  -  routed by `platformIdentityRouting`, see `$HOME/.claude/multi-agent-refs/setup/identity-routing.md`):
   ```
   git config user.name  "{identity.name}"
   git config user.email "{identity.email}"
   ```
5. Create log file: `$HOME/.claude/logs/multi-agent/{project}/{task-id}/agent-log.md` with header
6. Create state file: `$HOME/.claude/logs/multi-agent/{project}/{task-id}/agent-state.json` with:
   ```json
   {
     "taskId": "PROJ-12345",
     "branch": "feature/PROJ-12345-order-filter",
     "project": "my-ios-app",
     "currentPhase": 0,
     "status": "in_progress",
     "startedAt": "2026-04-07T09:30:00Z",
     "phases": {
       "0": { "status": "done", "duration": "5s" },
       "1": { "status": "pending" },
       "2": { "status": "pending" },
       "3": { "status": "pending", "todos": [], "retryCount": 0 },
       "4": { "status": "pending", "reviewIteration": 0 },
       "5": { "status": "pending" }
     },
     "analysisFindings": [],
     "planTodos": [],
     "reviewConsensus": null
   }
   ```
7. Log: `✅ Phase 0: Init complete`
