# Copilot CLI orchestrator - project detection, routing and phase summaries

Detail behind the Copilot CLI orchestrator skill (`skills/shared/core/multi-agent/SKILL.md`). The skill keeps the routing skeleton, the hard rules and the gates; this file carries the project table, the full routing rows and the per-phase routing summaries. Each phase's specification is `$HOME/.claude/multi-agent-refs/phases/phase-N-<name>.md`; where a summary here and a phase ref disagree, the phase ref wins.

## Project detection

Detect the project from cwd FIRST, then validate the input format:

| cwd contains | Project | Accepted Input | Rejected Input |
|--------------|---------|----------------|----------------|
| `/my-ios-app` | my-app | `"PROJ-XXX" "branch"` | GitHub Issue URL |
| `/my-figma-app` | figma | `"https://github.com/.../issues/N"` | Jira ID + branch |
| Other | generic | Both formats | - |

If the user gives the wrong format for the project, warn:

- my-ios-app + GitHub URL: "This project expects a Jira ID + branch. Example: multi-agent \"PROJ-12345\" \"feature/PROJ-12345-desc\""
- my-figma-app + Jira: "This project expects a GitHub Issue URL. Example: multi-agent \"https://github.com/my-org/my-figma-app/issues/316\""

## Input types

Five types, matching the `commands/multi-agent/SKILL.md` dispatcher contract (cross-CLI parity is enforced by `smoke-cross-cli-behavior.sh`):

| # | Pattern | Type | Action |
|---|---------|------|--------|
| 1 | `#N` or bare `316` | github-issue-number | Fetch via gh after project selection |
| 2 | `https://github.com/.../issues/N` | github-issue-url | Extract org/repo/N, fetch via gh |
| 3 | `https://{JIRA_HOST}/browse/{JIRA_KEY}-XXXXX` | jira-url | Extract ID, fetch via Jira API |
| 4 | `{JIRA_KEY}-XXXXX` (bare) | jira-id | Fetch via Jira API |
| 5 | Anything else | free-text | No external fetch |

All types enter the same Phase 0 flow.

## Routing rows

| Input Pattern | Action |
|---------------|--------|
| `"PROJ-XXX" "branch"` | my-app only: Jira ID + branch name, auto-assigned incremental ID |
| `"https://github.com/..."` | figma only: parse the issue, extract Jira from title/body, auto-create the branch |
| `status` | Show all tasks with ID, phase, status |
| `log [id]` | Show agent-log.md for the task (latest if no id) |
| `resume [id]` | Resume a paused/failed task from the last successful phase |
| `kill [id]` | User-only: name `/multi-agent-kill` for the user to type; that skill stops the task and deletes worktree + branch + logs after a confirmation |
| `clear-logs` | Superseded: name `/multi-agent-prune-logs` for the user to type |
| `purge` | User-only: name `/multi-agent-purge` for the user to type; that skill deletes ALL worktrees, branches, logs, state and the counter |
| `review` | Skip phases 0-3, review the current diff only |
| `autopilot` flag | Add to any pipeline command: skip user confirmations except destructive ops |
| `help` | Usage guide |
| No args | Ask (`_entry-picker.md`), then route the answer. A modifier with no task prints usage; an unattended run refuses |

Every task gets an auto-incremented short ID (`#1`, `#2`), stored in `agent-state.json`.

## Autopilot matrix

`multi-agent "PROJ-12345" "feature/PROJ-12345-desc" autopilot` skips interactive confirmations and runs end-to-end.

| Phase | Normal | Autopilot |
|-------|--------|-----------|
| Phase 1 (Plan Approval Gate) | Clarification (max 2 rounds) + approval loop; the user replies `onayla` / `iptal` / free-text | Gate skip (safety classifier did not pause): record it with `echo "{\"phases\":{\"1\":{\"gateSkippedAt\":\"$(date -u +%FT%TZ)\"}}}" \| node $HOME/.claude/scripts/write-state.mjs "$STATE_FILE"`, log the plan, run the plan attachment, go to Phase 2 |
| Phase 3 (User test) | "Want to test it?" then wait | Skip, straight to Phase 4 |
| Phase 4 (Commit) | "Want to commit?" then wait | Automatic commit + push |
| Phase 4 (PR) | "Want to open a PR?" then wait | Automatically create the PR |

`"autopilot": true` is added to `agent-state.json` and survives a resume.

## Persona models

| Persona | `preferredModel` |
|---|---|
| `explorer` | `sonnet` |
| `code-reviewer` | `fable` (Reviewer 1; Phase 3 sets Reviewer 2 `opus`, Reviewer 3 `sonnet`) |
| `security-auditor` | `opus` |
| `ios-architect` / `android-architect` / `backend-architect` | `fable` |

Each persona file carries its own `modelRationale`. Observability: emit a `phase.agent.dispatch` OTel span with `{persona, resolved_model, override_source}` when `MULTI_AGENT_OTEL_SPANS=1` (see `phase-tracker.sh`).

## Phase summaries

**Phase 1: Plan.** Two halves, one approval gate. 1a Analysis (`explorer`, Sonnet): parallel explore agents scan related files, patterns and impact areas, then log `Phase 1: Plan (analysis) - {N} files identified`. 1b Planning (Fable): task breakdown into todos with dependencies, `ios-architect` review for structural changes, an approach per todo, then the Plan Approval Gate (`phases/phase-1-plan.md` Step 13). Every write of `.pipeline/plan.json` (first draft, rework, edit) first clears the gate records: `echo '{"phases":{"1":{"planApprovedAt":null,"gateSkippedAt":null}}}' | node $HOME/.claude/scripts/write-state.mjs "$STATE_FILE"`. 5a clarification runs only when the description is ambiguous (vague acceptance, UI task without Figma, API task without an endpoint contract, `ambiguityScore >= 2`, parent-story scope drift), max 2 rounds, then a "best-effort" banner. 5b approval loop: `onayla` / `iptal` / free-text edit; an edit makes Fable revise and re-render, no iteration cap. Persist `clarificationRounds`, `clarificationQuestions`, `clarificationAnswers`, `planIterations`, `planApprovedAt`, `gateSkippedAt`, `planEditRequests` to `state.phases["1"]`. Once the gate passes (Approve, or the autopilot skip), run `node $HOME/.claude/scripts/plan-attachment.mjs decide --state "$STATE_FILE"`, then `plan-attachment.mjs upload --state "$STATE_FILE" --plan "$WORKTREE/.pipeline/plan.json"` when it says so (`features/plan-attachment.md`); it never stops the run.

**Phase 2: Dev (Sonnet).** Per todo in dependency order: mark `in_progress`, TDD (failing test, verify RED, minimal code, verify GREEN, refactor), build via a task agent, on failure fix and rebuild (max 3, `retryCount` in `agent-state.json`), mark `done`, log `Phase 2: {todo-id} - Test ok, Build ok`.

**Phase 3: Review (parallel + triage).** Step 0 diff risk scoring is advisory: `node $HOME/.claude/scripts/diff-risk-score.mjs --base "$BASE_BRANCH" --top 5`, injected as `${PRIORITY_FILES}`; off when `prefs.global.diffRiskAdvisory = false`. Reviewers by host: Claude Code runs `claude-fable-5-2` (security + architecture), `claude-opus-5-5` (edge cases), `claude-sonnet-5-5` (quality); Copilot CLI runs `gpt-5.4`, `claude-opus-4.7`, `claude-sonnet-4.6` (no Fable). Triage is one top-tier pass (`claude-fable-5-2` on Claude Code, `claude-opus-4.7` on Copilot CLI). Blocking findings go back to Phase 2 (at most `review-rounds.mjs get --state "$STATE_FILE" --value` rework rounds, 1 to 3, default 2), important ones are fixed and re-reviewed, suggestions applied when reasonable.

**Phase 4: Commit & PR.** Per-project steps and the GitHub issue body update: `orchestrator/phase-4-5-projects.md`.

**Phase 5: Report.** Finalize `agent-log.md` with: timeline table (phase, agent, model, duration, status, detail), agent activity report, token estimate, per-phase time distribution, review iterations, files changed (`git diff --stat`), review consensus, and test scenarios in Turkish for Jira (precondition, steps, expected result). Then print a compact terminal summary: task id and status, total time, agent calls, files changed, commit and PR, the wiki page when one was pushed, the plan line from `plan-attachment.mjs line --state "$STATE_FILE"` (only when it prints one; it prints nothing on a free-text run), and the path to the full report under `~/.claude/logs/multi-agent/{project}/{task-id}/agent-log.md`. Log `Phase 5: Report complete`. The wiki + Figma screenshot step runs only for a component implementation with a GitHub issue: `orchestrator/phase-4-5-projects.md`.

**Localization scope.** Phase labels are English on every host. Reviewer-count qualifiers such as `(parallel + triage)` stay English. Out of scope for localization (always English, per `prefs.schema.json`): reviewer and triage system prompts, commit messages, PR titles and bodies, Jira comments, wiki content and log payloads inside `agent-log.md`.
