# Phase 4 Commit  -  Multi-Repo Mode

> Loaded on demand from `phases/phase-4-commit.md`. Conditional on
> `state.projects[].length > 1`; a single-repo run never needs it. The
> Push-must-succeed policy stays in the phase doc because every run uses it.

Active when `state.projects[].length > 1`. The single-repo flow in the phase doc is preserved verbatim  -  multi-repo is an extension, not a replacement.

## Per-repo commit (shared message, per-repo scope)

For each repo in `state.projects[]` (sequentially, in dependency order from Phase 1):

1. `cd "$WT_PATH"`  -  switch into the repo's worktree.
2. Build the commit message using the **shared subject** + **per-repo scope** rule:
   ```
   {type}({repo-scope}): {shared subject} [{tracker-id}]

   Part of multi-repo change  -  see sibling PR(s) in repo `{other repo names}`.
   ```
   `repo-scope` is the repo's short name (e.g. `common`, `uicomponents`). The subject and `[tracker-id]` are identical across all repos.
3. Fold this repo's checkpoint commits first: `bash $HOME/.claude/scripts/checkpoint-fold.sh --repo "$WT_PATH" --task "{tracker-id}" --base "$BASE_BRANCH"`. Then `git add` (specific files only  -  same rules as the single-repo staging step, item 4 of the Standard path in `phases/phase-4-commit.md` Step 2)
4. `git -C "$WT_PATH" commit -m "$MESSAGE"` (heredoc body)  -  using identity already pinned in Phase 0 Step 8.
5. Capture `state.projects[i].commit = {"sha": "<sha>"}`.

Push each repo through the Push-must-succeed policy in `phases/phase-4-commit.md`, then continue here.

## Per-repo PR with cross-links

After all repos are pushed, open PRs sequentially, one per `state.projects[]` entry, each through its remote's usual path (gh / Bitbucket REST) with the per-stack body template (Standard / UI Component / etc). Append to each body:

```
## Sibling PRs (multi-repo task)
This PR is part of a multi-repo change. Sibling PRs (review together):
- <url of each PR opened before this one>
```

After the last PR opens, `update_sibling_links`: go back to the first N-1 PRs and add the sibling URLs that were not yet known when they were created.

**Then finalize each repo's worktree**, serially and only now  -  `update_sibling_links` updates one PR per repo and the loop `cd`s per repo, so removing repo 1's worktree mid-loop breaks 2..N. Run `worktree-finalize.sh` once per repo with that repo's `--worktree` / `--project-root` / `--project`; each skips or removes independently.

`update_sibling_links` does an UPDATE call per PR (Bitbucket: the `channels/pr.md` PUT payload, current `version`; GitHub: `gh pr edit --body-file`). Idempotent.

## GitHub issue body  -  list all PRs

Step 10's "Issue body update" extends to multi-repo: instead of a single `### Pull Requests` line, write one row per repo:

```
### Pull Requests
- **common:** {pr-1-url}
- **uicomponents:** {pr-2-url}
```

Match the existing template's table/list shape  -  do not introduce new markers (per existing single-repo rule).

## `recentGroups` update on success

After ALL repos in the task have `pushStatus ∈ {"pushed", "rebase-pushed"}`:

1. Find the matching `recentGroups[]` entry by **order-independent compare** of `repos` array (sort + JSON.stringify).
2. If found: `entry.count += 1`, `entry.lastUsed = ISO timestamp`, bump to front of array (LRU).
3. If not found: prepend a new entry `{ label: <auto-generated>, repos: [...], count: 1, lastUsed: ... }`. Cap the array at 10 (drop oldest).

A task ending with one or more `pushStatus === "skipped"` repos does NOT bump `recentGroups` count  -  only fully-successful multi-repo tasks earn LRU bumps.
