--- name: merge-pull-request description: Merge an already-green PR (rebase merge, delete branch) and fast-forward the local main checkout for HMR. This is the Merge column skill. It assumes the Testing column (/pull-request) already drove the PR to green. Not for creating a PR (use /pull-request) or a direct quick-push (use /merge-back). allowed-tools: Read, Glob, Grep, Edit, Write, Bash(git:*), Bash(npm:*), Bash(gh:*), Agent, mcp__kangentic__kangentic_get_current_task, mcp__kangentic__kangentic_link_pr --- # Merge Pull Request Merge a green pull request and pull the result back into the local `main` checkout so the dogfooding `npm start` picks it up via HMR. This is the **Merge column** skill. It assumes the **Testing column** (`/pull-request`) already created the PR and drove its CI checks to all-green and flake-free. It verifies the CI checks are green, then merges with `--admin` to waive the review requirement: `main` requires one approving review, but a maintainer's own PRs get no second reviewer, so that bypass is the normal Merge path. It NEVER bypasses the CI checks - those are confirmed green first; the `--admin` only waives the missing review. (For a deliberate direct quick-push that skips the whole PR gate, use `/merge-back` instead.) A run that merged and finished ends on the line `PR: merged`, and the Merge column's "Finish" turn-end automation (`kangentic.json`) reads that line and moves the task to Done. A run stopped by red CI ends on `PR: checks red`, and the "Back to Testing" automation sends the task back to Testing, where `/pull-request` fixes the checks and sends it back here. See "Outcome line" below. **This skill merges.** By the time a task reaches Merge the decision to ship has been made, and this skill executes it. Red CI is the one thing that sends the task away, and it goes back to Testing on its own, because fixing and de-flaking checks is `/pull-request`'s job. Anything else it finds - a stale doc, a bad comment, a lint nit - it **fixes here and then merges**, accepting a second CI round as the price. It never ends with a green PR still open, and it waits for a person only when a person has to decide something: then it ends on `Needs owner: `. If you are about to stop with a green PR and no question, you are doing the wrong thing. **Usage:** `/merge-pull-request` ## Pre-flight Checks All git commands run from the **current working directory** - never `cd && git ...`. Use `git -C ` to target another directory. A stop in these checks is a hold: report it and end on `Needs owner: `. 1. **Detect mode:** worktree mode requires CWD to contain `.kangentic/worktrees/`. If this is the main repo (no worktree), stop and tell the user this skill runs from a task worktree (the Merge column); a direct push from the main checkout is `/merge-back`. 2. Get the current branch: `git rev-parse --abbrev-ref HEAD`. If `HEAD` (detached), warn and stop. 3. Derive the project root: two directories above `.kangentic/worktrees//` (strip `.kangentic/worktrees/` from the worktree path). 4. Determine the source branch: `git config kangentic.baseBranch` (fallback: `main`). A task based on a tag or a commit has no branch to merge into. When `git show-ref --verify --quiet refs/remotes/origin/` fails, use origin's default branch (`git symbolic-ref --short refs/remotes/origin/HEAD`, minus the `origin/` prefix) and say so in one line. When `origin/HEAD` is not set (`symbolic-ref` fails), use `main`, or `master` when only that one is on origin. 5. Verify the GitHub CLI is authenticated: `gh auth status`. If it fails, report it and stop. ## Step 0 - Resolve the PR (by stored number first, head branch as fallback) The PR's head branch may NOT equal the worktree's local branch. `/pull-request` pushes the unchanged local branch to a clean PUBLIC remote name (`/`) and opens the PR from THAT, keeping the local branch as the session-safe slug-hex (see `/pull-request` Step 1.5). So resolve the PR the way `src/main/pr/pr-linking.ts` does - by the stored `pr_number` first (branch-independent), falling back to the head branch only when there is no stored number. 1. `` is the current LOCAL branch from pre-flight. It is used later ONLY for local git operations (the worktree realign), NEVER for `gh pr` lookups. 2. Resolve the PR number ``: - Call `kangentic_get_current_task` (it reads the worktree's task) and take its `pr_number`. If present, that is `` (the reliable, branch-independent path). Also record the returned task's ID as `` - it is reused in Step 3 to force an immediate board refresh after the merge. If no task is found, `` stays unset and that refresh is skipped. Note the task's column too, the name in brackets after its title. Step 5 reads it. - If absent, fall back to the head branch: `gh pr list --head --state open --json number` (covers older PRs where the local branch IS the head); `` is the first match's number. When that finds none, run `gh pr list --head --state merged --json number` once. It finds a PR an earlier run already merged, which item 3 handles. 3. Run `gh pr view --json number,url,state,mergeable,mergeStateStatus,statusCheckRollup,headRefName`. Record `` = `headRefName` (the PR's REMOTE head branch - the push and merge target). **If `state` already reads `MERGED`, an earlier turn merged it** (one that stopped in the realign or in Step 4, or a re-run). Do not rebase, push or merge again: Step 2's push to `` would recreate the head branch that `--delete-branch` removed. Skip to "Refresh the board's PR status" in Step 3 and continue from there, with the `` the earlier turn recorded. When no turn in this conversation recorded one, use the rebase realign: it is this repo's default merge, and it aborts cleanly on a conflict. 4. If no PR resolves either way, stop and report that the Testing column should have created one (run `/pull-request` from the Testing column first), ending on `Needs owner:`. Do not create a PR here. Every later `gh pr` command (view, checks, merge) targets `` or ``; the local `` is for local git only. **Fork PR.** When `gh pr view --json isCrossRepository` reads true, the head is on a contributor's fork, and the steps below change in three places: - **Every push** (Step 1 item 4, Step 2 item 2) is a bare `git push` from the branch `gh pr checkout ` created, after the two checks in `/pull-request`'s "Fork PRs" section: the fork's current head is an ancestor of `HEAD`, and `git push --dry-run` names the fork and ``. Never `git push origin ...`, which creates a stray branch here that the PR never sees. - **Step 2 item 2 merges instead of rebasing:** `git merge origin/`. Never rewrite or force-push the contributor's commits. - **Step 3 squashes, without `--delete-branch`:** write a short English body to `.kangentic/SQUASH_BODY.tmp`, then run `gh pr merge --admin --squash --subject " (#)" --body-file .kangentic/SQUASH_BODY.tmp`. Without a body GitHub joins every commit message, and a contributor's may not be in English. The head branch belongs to the contributor, so leave it. Record `` = squash. ## Step 1 - Doc backstop (fix anything it finds, here and now) The doc audit's real home is `/pull-request`, BEFORE the PR is created (its Step 1 item 3, which runs against the branch diff even when the tree is clean). This step is a cheap BACKSTOP for what that pass missed. **If the backstop finds a gap, FIX IT HERE.** Do not bounce the task back to Testing, do not file a follow-up task, do not merge around it, and do not end the run with the PR still open. Edit the doc, commit it, push it, wait out the CI round it re-triggers, and then merge. **Running CI twice is an accepted cost** - it is cheaper than shipping a doc that lies, and far cheaper than a human round-trip through the board to fix one sentence. This was learned from a real Merge run that stalled: the backstop found a genuinely stale sentence, refused to merge, and told the user to move the task back to Testing. The finding was right and the refusal was wrong - it left a 24/24-green PR sitting open over a one-clause doc edit this skill was perfectly able to make. 1. Determine the anchor source files in the branch diff (compare the current diff against `origin/`), then narrow to files matching the canonical anchor list in `.claude/skills/sync-docs/SKILL.md` Step 2. If none, skip to Step 2. 2. Spawn a `doc-auditor` agent. Ask it BOTH questions: whether the docs still enumerate every anchor, AND whether any prose describing this diff's behavior has gone stale. The second is outside the agent's default contract (`.claude/agents/doc-auditor.md`: "Ignore prose"), so it answers it only when asked. Give it a prose summary of what each changed file now does, not a bare file list. 3. If it reports no gap, go to Step 2. This is the expected outcome. 4. If it reports a gap, **verify it yourself** by reading the cited `file:line` - a prose finding is the agent working outside its contract, so confirm the sentence is actually stale before editing. Then fix it with `Edit`, commit with a `docs:` message via `.kangentic/COMMIT_MSG.tmp`, and push to the PR head: `git push origin HEAD:`. **Plain push, no `--force-with-lease`.** This adds a commit rather than rewriting history, so no force is needed - and a lease would likely be REJECTED here anyway, because pushing via `HEAD:` never updates the local remote-tracking ref for ``, leaving the lease stale. Reach for `--force-with-lease` only after a rebase (Step 2 item 2), and `git fetch` first to refresh the lease. Step 2 item 4 then waits out the re-triggered CI before the merge. That is expected, not a failure. ## Step 2 - Re-verify (rebase if main moved, confirm green and mergeable) 1. `git fetch origin `. 2. If `` moved since the PR went green, rebase onto it: `git rebase origin/`. Resolve conflicts the same way `/pull-request` does. A conflict that needs a person's judgment (two changes that each make sense and cannot both stand) is a hold: `git rebase --abort`, report the files and the choice, and end on `Needs owner: `. If the rebase changed history, push the local HEAD to the PR's remote head: `git push origin HEAD: --force-with-lease`. 3. Re-read the PR state: `gh pr view --json mergeable,mergeStateStatus,statusCheckRollup`. **Require EVERY check in the rollup to be green (SUCCESS)** - the whole set, not the branch-protection-required subset. That is the real gate; do not rely on the merge command to enforce it. **Never gate on `gh pr checks --required`.** This repo requires five checks (`cla`, the one job named `Lint, Typecheck, Build`, and the unit, UI and E2E gates), so `--required` reports all-green while the web demo and any path-filtered check are still running. When only four were required it happened once: `--required` said green with 16 checks in flight, and an `--admin` merge at that moment would have waived all 16. Use `gh pr checks --json name,state` and confirm the count of SUCCESS equals the total. `mergeStateStatus` will usually read `BLOCKED` rather than `CLEAN` here because the maintainer's own PR has no approving review. It reads `BEHIND` instead when another PR landed on `` and step 2's rebase was skipped or did not catch it, since this repo requires branches to be up to date. Both blocks are EXPECTED and both are waived by the `--admin` merge in Step 3, which is why the board chip reads `ready` for either. Do not stop on them. But if a CHECK is failing, pending or missing (not merely the review or a stale base), never `--admin` past it: send a red one back now (item 5), and wait for a pending or missing one (item 4). 4. Wait when any check is pending or missing, or when the rebase (step 2) or Step 1's doc push re-triggered checks. For a few seconds after a push the rollup is empty or partial while GitHub registers the new head's check suites, so first re-read `gh pr view --json headRefOid,statusCheckRollup` until the rollup lists checks for the pushed head. Stop after 10 reads: a check that has not registered by then is missing. Then watch the checks with `gh pr checks --watch --fail-fast --interval 30` (Bash `timeout` about `2400000` ms), and read the rollup again as item 3 says. 5. **Red CI goes back to Testing.** When any check in the rollup is red, or is still pending or missing after the wait in item 4, do not fix tests here and do not wait for a person. A flaky spec, a flake that came in from main and a real regression are all `/pull-request`'s to fix. Testing sends the task back here once the checks are green. Report each red, pending or missing check with its run ID, and name the failing specs when `gh run view --log-failed` shows them. Leave the PR open with no half-finished rebase, and end the message on the line `PR: checks red`, alone. The board, not this skill, counts the round trips (see "Outcome line"). ## Step 3 - Merge the PR Only after Step 2 confirmed every required CHECK is green, merge with the maintainer bypass that waives the missing review: Run: `gh pr merge --admin --rebase --delete-branch` - `--admin`: waives the required approving review (the maintainer's own PR gets no second reviewer), and with `--rebase` it also lands a branch that is `BEHIND`, since the rebase replays the commits onto the current base tip. It does NOT relax the CI gate - Step 2 already verified the checks are green; this only clears the protection blocks. NEVER run it without that green-check verification (it would also bypass the checks). What the rebase does not do is re-run the checks against the new base, which is what an up-to-date-branch rule exists for; Step 2's rebase is the place to force that when it matters. - `--rebase`: lands the individual commits on the source branch (no merge commit). - `--delete-branch`: deletes the remote PR head branch (``). The local `` has a different name (the slug-hex), so gh's local-branch delete is a no-op and the worktree branch stays for the realign below. For a local branch that IS the PR head (an older PR, or a release branch), gh also tries to check out the base branch locally. That runs AFTER the merge and the remote delete, and it fails with `'main' is already used by worktree`. **A non-zero exit is not proof the merge failed.** So on any `gh pr merge` error, read the state before deciding: `gh pr view --json state`. If it reads `MERGED`, the merge landed: carry on to the board refresh and the realign below with the merge method you attempted. If it does not, the error is real, and the two paragraphs below say what to do. **Merge-method fallback - if `--rebase` fails with "can't be rebased":** the PR history contains a merge commit (typically from a mid-task `git merge origin/` that integrated the base with conflict resolutions). GitHub cannot linearize that, so a rebase merge is impossible. Fall back to a SQUASH, which still keeps `` LINEAR and lands the feature as one commit (the granular history stays preserved in the PR): `gh pr merge --admin --squash --delete-branch`. Do NOT use `--merge`: a merge commit would break the linear-`main` convention this repo maintains. Record `` (rebase or squash) - it selects the realign below. **If the merge fails** for any OTHER reason than the expected missing-review block, do NOT force past it. When the reason is a check that went red, this is Step 2 item 5: report it and end on `PR: checks red`. Any other refusal is a hold: report the unmet requirement and end on `Needs owner: `. ### Refresh the board's PR status The board caches each task's PR state and only re-resolves it on a background timer (default 5 min) or on project open, so the Merge card would otherwise keep showing "PR # open" for minutes after this merge. Force an immediate re-resolve so the card flips to "merged" right away: - If Step 0 resolved a ``, call `kangentic_link_pr` with that task ID. It re-resolves the PR by number (branch-independent, so it works after `--delete-branch`), writes the fresh `merged` state, and pushes the update so the board card re-renders at once. - If no `` was resolved (the worktree has no linked task), skip this - there is no board card tracking the PR, so nothing is stale. Run this right after the merge succeeds, before the realign below, so the board updates even if the realign later needs to stop for a conflict. ### Realign the worktree branch (so move-to-Done reads clean) The merge rewrote the PR commits onto `` (new SHAs for a rebase merge, or a single new squash commit), so the worktree's local branch still holds the pre-merge commits. Left as-is, the board's move-to-Done check reports them as "N commits remain only on the local branch" - a false positive. Realign the worktree branch onto the merged base, by ``. Every "report" below is a hold that ends on `Needs owner: `: 1. Confirm the worktree is clean: `git status --porcelain` (empty right after the merge). If it is NOT empty, skip the realign and report - never discard uncommitted work. 2. `git fetch origin ` to refresh the merged base. If `git rev-list --count origin/..HEAD` now reads 0, an earlier turn already realigned the branch: skip to Step 4. 3. Realign by merge method: - **rebase merge:** `git rebase --no-keep-empty --force-rebase origin/`. Git drops the now-merged commits (matched by patch-id) and replays only any genuinely-unmerged local commits, leaving the branch at the merged HEAD (plus any real leftover work, which item 5 reports). `--no-keep-empty` also drops a commit that started empty, such as the `chore(review)` ledger commit `/code-review` makes, which git otherwise keeps. GitHub's rebase merge already left that commit off ``, and it has no files, so dropping it cannot strand work. `--force-rebase` makes git replay a branch that already sits on the merged base, where a plain rebase reports it is up to date and keeps the empty commit. - **squash merge:** the local commits do NOT patch-match the single squash commit, so a rebase would not cleanly drop them. Since step 1 confirmed the worktree is clean and every change is now in the squash on ``, reset the branch to the merged base instead: `git reset --hard origin/`. This is safe PRECISELY because the worktree is clean and the content is fully merged; never reset if step 1 found uncommitted work. 4. On a rebase conflict (rare - only a genuinely-unmerged local commit can clash), abort cleanly: `git rebase --abort`, then report. Never leave a half-finished rebase in the worktree. 5. Confirm nothing is left only on the local branch: `git rev-list --count origin/..HEAD` must read 0. If it does not, list the leftover commits with `git log --oneline origin/..HEAD`. When every one is an empty `chore(review)` ledger commit (`git show --stat ` lists no files), item 3 ran without its two flags. Run item 3 as written, then this item again, and do not ask the user. Report any other leftover commit. A move to Done keeps the branch but archives the task, and an automated move shows none of the warnings a drag does, so this work would sit in Done with nobody told. ## Step 4 - Pull back into the local main checkout The project root is the dogfooding checkout (`npm start` serves the main process from it), so keep it fast-forwarded to the freshly merged `main`. This step is salvaged from `/merge-back` and handles divergence loudly rather than letting it compound. 1. Record where it stands, `git -C rev-parse HEAD` (call it ``), then fast-forward it: `git -C pull --ff-only`. If this succeeds, run `git -C diff --name-only HEAD -- src/main src/preload src/shared src/devtools packages/protocol/src scripts package-lock.json` and note what it listed, then go to Step 5. Never restart the app from this skill: a restart suspends every running agent, this one included. 2. **If it fails, do NOT just log a soft warning - that is how divergence compounds.** A fast-forward is impossible the moment the local source branch has even one commit the remote lacks. Diagnose and surface it: a. Run `git -C status -sb` to read the ahead/behind counts. b. If the local branch is **behind only** (ahead 0) and the ff still failed, the working tree likely has uncommitted changes. Report that and stop - do not stash or discard the user's work. c. If the local branch is **ahead** (has unpushed local commits), list them with `git -C log --oneline origin/..` and name them. These block every future ff-pull until reconciled. 3. **Offer to reconcile the ahead case** (do not do it silently): - Rebase: `git -C rebase origin/`. - If the user wants those commits upstream, push: `git -C push origin `. - **On conflict, abort cleanly:** `git -C rebase --abort`, then report manual steps. NEVER leave the running dogfooding checkout in a half-finished rebase. The remote `main` is already updated by Step 3, so a failure here is non-fatal to the merge - but it is not "ignore and move on": an un-surfaced local commit will keep breaking this step. **Prevention:** the local `main` checkout should only ever fast-forward. Do not commit directly to it - use a worktree or feature branch. A stop in this step is a hold and ends on `Needs owner:` (see "Outcome line" below). When a later turn finishes the reconciliation and `git -C status -sb` no longer reads `behind`, end that turn with the Step 5 report and its outcome line. ## Step 5 - Report Summarize: - PR URL (with link) and the branch that was merged. - The source branch that received the changes and the number of commits landed. - Branch cleanup status: the remote PR head (``) deleted by `--delete-branch`; the local worktree branch (``, a different name) realigned to the merged base. The move to Done later removes the worktree and keeps that branch. - Local `main` checkout status (fast-forwarded for HMR, or the divergence you surfaced). - Only when Step 4's diff listed files, one line. A file outside `scripts/` other than `package-lock.json`: main-process code changed, which HMR cannot load; the dogfood window may offer a restart ("Main process changed"), as it does whenever the built main, preload or worker bundle differs. `scripts/dev.js`, or a `scripts/` module it `require`s: the dev server itself changed, which an in-app restart does not reload, so `npm start` needs a stop and start. Any other `scripts/` file needs no line. - Only when Step 4's diff listed `package-lock.json`, one line: the main checkout's `node_modules` is behind the lockfile and needs `npm install` in ``, which updates `node_modules` in place where `npm ci` deletes it first, under every preview worktree linked to it. A running `npm start` must stop first and start again after. Never run the install from this skill, for the same reason it never restarts the app. - Only for a run outside the Merge column, a reminder to move the task to Done, which removes the local worktree. Step 0 noted the task's column. A column other than Merge, or no task at all, is outside it. In the Merge column, leave the reminder out: the line below moves the task. - Then, only when "Outcome line" below allows it, the last line of the message, alone: `PR: merged`. On the board that moves the task to Done. ## Outcome line The Merge column's two turn-end automations (`kangentic.json`) are Text matches: each time a turn ends here, they read the last line of your final message that still has a word in it, ignoring case, punctuation and markdown. "Finish" moves the task to Done when that line reads `PR: merged`, and "Back to Testing" moves it to Testing when it reads `PR: checks red`. Done removes the worktree, so the lines are a contract. This section and those rows change together (`tests/unit/board-turn-end-contracts.test.ts` fails when they drift): - `PR: merged` comes only from Step 5, and only when all three hold: - **The PR is merged.** `gh pr view --json state` reads `MERGED`. Read it; never infer it from `gh pr merge`'s exit code (Step 3 says why). - **The realign finished clean.** `git status --porcelain` is empty and `git rev-list --count origin/..HEAD` reads 0. A realign skipped for uncommitted work leaves work that Done's worktree removal destroys. One that aborted on a conflict, or that replayed a leftover commit, would archive commits that never landed with nobody told. - **Step 4 fast-forwarded the main checkout.** When Step 4 stops to report, or offers to reconcile the ahead case, end on `Needs owner:` instead. The row checks again at every turn end until it runs, so the turn that finishes the reconciliation ends on `PR: merged`. - `PR: checks red` comes only from Step 2 item 5, and the Merge column's "Back to Testing" row reads it and moves the task to Testing. That covers any check in the rollup that is red, or still pending or missing after the wait, and a merge refused because a check went red. The task needs nobody: Testing's `/pull-request` drives the checks green and its `PR: ready to merge` line brings the task back here. The row's Frequency (`maxRuns` in `kangentic.json`) caps how often it runs on a task, so a `` that stays red cannot bounce it forever. A red stop after the row is spent leaves the card in Merge, and the board marks it stopped with that reason. This skill counts nothing: print the line every time. - Every other stop is a hold, because a person has to decide or act. End it on `Needs owner: `, which matches neither row, so the card stays in Merge and the board marks it stopped at once. The holds: the pre-flight stops (main repo mode, a detached HEAD, a failed `gh auth status`), no PR resolved in Step 0, a Step 2 rebase conflict that needs judgment, a merge refused for anything but the missing review or a red check, and the realign and Step 4 stops above. The `PR:` prefix marks a pull request outcome, shared with Testing's `PR: ready to merge`. Code Review ends on `Verdict: Ready`. ## Rules **CRITICAL: No chained commands.** Every Bash call must contain exactly ONE command. Never use `&&`, `||`, `|`, or `;`. For git commands in another directory, use `git -C ` - never `cd && git ...`. Conventional commit messages. No em-dashes or `--` as punctuation.