# Link

Attach a plan file to the workspace and create beads issues for each phase, as children of the branch epic. Run this after writing or generating a plan.

This mode is self-contained — it does NOT require `create-plan` to have been used. The plan can come from anywhere: manually written, generated by any tool, or copied in.

## Steps

1. Run **check** first to confirm the slot exists.

2. Get repo URL and branch slug:
   ```bash
   repo_url=$(git remote get-url origin 2>/dev/null | sed 's|https://||;s|git@||;s|\.git$||')
   branch=$(git rev-parse --abbrev-ref HEAD)
   branch_slug=$(echo "$branch" | sed 's|/|--|g')
   branch_dir=~/.whisper/repos/"${repo_url}"/branches/"${branch_slug}"
   context_path="$branch_dir/context.md"
   plan_path="$branch_dir/plan.md"
   ```

3. Determine the plan file path:
   - If the user provided a path (`/w link path/to/plan.md`): use that.
   - Otherwise: use `plan.md`.
   - If neither exists: tell the user to write a plan first and stop.

4. If the source path is not `plan.md`, copy it in:
   ```bash
   cp <source-path> "$branch_dir/plan.md"
   ```

5. Read `plan.md` and extract phase headings.

6. Ensure the branch epic exists:
   - If `context.md`'s `beads-epic:` is already set, use it.
   - If blank and the plan has more than one phase: create it.
   - If blank and the plan is a single task: skip the epic.

7. For each phase/task, create a beads issue as a child of the branch epic:
   ```bash
   bd create "<phase-title>" --type task --parent <epic-id>
   ```

8. Only if phases are genuinely sequential, chain dependencies:
   ```bash
   bd dep <id-N> --blocks <id-N+1>
   ```

9. Update `context.md` — set `beads-epic:` to the epic (or single-ticket) ID.

10. **After each successful creation**, extract the `**Files / Systems:**` bullet list from the issue description and store it as structured metadata:
    ```bash
    bd update <id> --metadata '{"files": ["path/to/file1.py", "path/to/file2.py"]}'
    ```
    This enables automated file-conflict detection by concurrent agents. If the Files/Systems section is empty or contains only subsystem names without concrete paths, **stop and ask for specific file paths** — a ticket without concrete paths cannot participate in conflict detection.

11. Confirm:
    ```
    Linked plan to workspace: branches/<slug>/
    Epic: <epic-id> — <branch>
    Created <n> child issues:
      <id>  Phase 1: ...
    ```

### Relinking

If `context.md` already has a `beads-epic:` set, ask before creating new children:
```
context.md already links epic <id> with <n> children.
Add new phases as more children of the same epic? [Y/n]
```