/agent-traffic-control:stacked-pr-base-branch-deletion-auto-closes-dependentfrom agent-traffic-control
Recover from the trap where deleting a base PR's branch auto-closes any open dependent stacked PR, and the closed PR cannot be reopened or retargeted. Use when: (1) you set up a stacked PR pair (PR2's `base` field = PR1's branch instead of `main`), (2) you merged PR1 via squash, (3) PR1's remote branch got deleted — via ANY route: `gh pr merge <N> --squash --delete-branch`, `gh api -X DELETE refs/heads/<branch>`, or `gh pr merge` followed by separate branch cleanup — (4) the dependent PR2 is now reported as `state: CLOSED` even though you never closed it, (5) `gh pr reopen N` fails with `Could not open the pull request`, (6) `gh pr edit N --base main` fails with `Cannot change the base branch of a closed pull request`. The only recovery for the stacked-PR case is to open a fresh PR from the same head branch with base=main. **v1.2.0 (2026-05-19) adds the recoverable single-PR variant**: if the deleted branch was the HEAD of a single, in-flight, NEVER-MERGED PR (e.g. user deleted the branch after a failed merge attempt that returned `GraphQL: Pull Request is not mergeable` because `mergeable: UNKNOWN`), the PR closes with `mergedAt: null, mergeCommit: null` and is RECOVERABLE via `git push origin <branch>` + `gh pr reopen <N>` — no fresh PR needed. Root-cause prevention: never `gh api -X DELETE` a branch until `gh pr view <N> --json state` reports exactly `MERGED`. Sister skill to `gh-pr-merge-worktree-checkout-trap` (one upstream cause).
Trending skills
7-day visitors to each skill page
Ships in agent-traffic-control. Installing the plugin adds it to Claude Code.
What the skill costs in Claude's context, stage by stage
Name and description, though model invocation is off: only you can start it.
SKILL.md joins the conversation and stays.
None: SKILL.md is the whole skill.
You have a stacked PR pair:
base: main, head: feature-pr1base: feature-pr1, head: feature-pr2 (stacked on PR1)You squash-merge PR1. The merge succeeds. You then run:
gh api -X DELETE repos/<owner>/<repo>/git/refs/heads/feature-pr1
…to clean up the orphaned remote branch (because gh pr merge --delete-branch
failed locally with the worktree-checkout-trap and you used the API as a
workaround). Within seconds, PR2 silently transitions to state: CLOSED:
$ gh pr view <PR2> --json state,closed,mergedAt,baseRefName
{"baseRefName":"feature-pr1","closed":true,"mergedAt":null,"state":"CLOSED"}
You never closed PR2. The PR's head branch still exists with all the work.
But:
$ gh pr reopen <PR2>
API call failed: GraphQL: Could not open the pull request. (reopenPullRequest)
$ gh pr edit <PR2> --base main
GraphQL: Cannot change the base branch of a closed pull request. (updatePullRequest)
GitHub refuses to reopen the PR (its base branch no longer exists), and refuses to retarget a closed PR. The PR is effectively a dead reference — all comments, reviews, and history land on a record that nothing can resurrect.
When a PR's base branch is deleted, GitHub's behavior is asymmetric:
main).DELETE /repos/.../git/refs/heads/<branch>), GitHub does NOT auto-retarget.
Instead, it auto-closes any open PR whose base field referenced the now-
missing branch.The closed PR cannot be reopened because GitHub validates that the base ref exists at reopen time. It cannot be retargeted because the reopen happens before the retarget — both are blocked by the missing-base validation.
You are in this trap if all of these are true:
--base <PR1-branch>,
not main).state: MERGED).gh pr merge <N> --squash --delete-branch (the normal merge-and-cleanup)gh api -X DELETE repos/<owner>/<repo>/git/refs/heads/<branch> (the
workaround when gh pr merge --delete-branch failed locally with the
worktree-checkout-trap)state: CLOSED with mergedAt: null and closed: true.gh pr reopen <PR2> fails with Could not open the pull request.The dead PR cannot be recovered. Open a fresh PR from the same head branch
against main:
# 1. Verify the head branch still exists with your work
git fetch origin
git ls-remote origin refs/heads/feature-pr2
# should print a SHA — your work survives on the remote branch
# 2. (Optional) Verify HEAD diff against main is what you expect
git log origin/main..origin/feature-pr2 --oneline | head
# 3. Open a fresh PR with base=main; reuse the original PR's body
# (you can copy-paste from the closed PR; comments don't migrate).
gh pr create \
--base main \
--head feature-pr2 \
--title "feat(...): same title as the dead PR" \
--body "$(cat <<'EOF'
Replaces #<DEAD_PR_NUMBER> (auto-closed when PR1's base branch was deleted
post-merge — GitHub couldn't auto-retarget). Same diff, now based on main
directly.
[... rest of original body ...]
Code-review findings preserved as comments on the closed PR:
https://github.com/<owner>/<repo>/pull/<DEAD_PR_NUMBER>#issuecomment-<ID>
EOF
)"
# 4. Squash-merge the new PR as usual
gh pr merge <NEW_PR> --squash
Reviews and comments on the dead PR are NOT auto-migrated. Leave a comment on the new PR that links to the dead PR's review thread so the audit trail stays connected.
Same mechanism, different scenario, different recovery:
You have a SINGLE PR (not stacked). You try to merge it. GitHub returns
GraphQL: Pull Request is not mergeable (mergePullRequest) because the
PR's mergeable: UNKNOWN, mergeStateStatus: UNKNOWN — GitHub hadn't
finished computing mergeability for a recent push. You assume the merge
succeeded silently anyway and proactively run gh api -X DELETE repos/<owner>/<repo>/git/refs/heads/<branch> to "clean up". The PR
state flips to CLOSED with mergedAt: null, mergeCommit: null — the
branch deletion auto-closed the PR before any merge happened.
Unlike the stacked case above, this is recoverable because:
gh pr reopen <N> on a PR closed by branch deletion
(it doesn't apply the "cannot reopen merged PRs" rule here because
the PR isn't merged — just closed)Recipe:
# Verify: PR is CLOSED but never merged
gh pr view <N> --json state,mergedAt,mergeCommit
# {"state": "CLOSED", "mergedAt": null, "mergeCommit": null}
# 1. Re-push the local branch (it's still in your worktree)
git push origin <branch>
# remote: ... * [new branch] <branch> -> <branch>
# 2. Reopen the PR — this works because PR was closed-not-merged
gh pr reopen <N>
# ✓ Reopened pull request ...#<N>
# 3. Wait for mergeability to compute
sleep 5
gh pr view <N> --json mergeable,mergeStateStatus
# {"mergeStateStatus": "CLEAN", "mergeable": "MERGEABLE"}
# 4. Now merge normally
gh pr merge <N> --squash --subject "..." --body "..."
The PR's full history (commits, comments, reviews, labels) survives the close-and-reopen cycle intact. No need to create a fresh PR.
Root-cause prevention for this variant: never call gh api -X DELETE
on a branch until gh pr view <N> --json state reports MERGED (not
OPEN, not CLOSED, not UNKNOWN). A failed merge attempt that
returns mergeable: UNKNOWN is GitHub still computing — wait, retry the
merge, don't take recovery actions yet. Quick check:
gh pr view <N> --json state --jq .state # must be exactly "MERGED"
Then and only then is the branch safe to delete.
Pick ONE of these patterns when working with stacked PRs + worktrees:
Don't delete PR1's branch until PR2 is also merged. After PR1 squash-merges,
GitHub automatically marks PR1's branch as "ready to delete" but doesn't
delete it. Leave it alone. Merge PR2 (which still has base: PR1-branch).
Once both are merged, clean up:
gh api -X DELETE repos/<owner>/<repo>/git/refs/heads/feature-pr1
gh api -X DELETE repos/<owner>/<repo>/git/refs/heads/feature-pr2
This is the cheapest and safest pattern. PR2 doesn't need to be retargeted —
it merges with base: feature-pr1, and GitHub computes the diff correctly
(PR2's head minus PR1's head, which is just PR2's specific commits).
If you need to delete PR1's branch immediately after merge (e.g., automation constraints):
# 1. Squash-merge PR1
gh pr merge <PR1> --squash
# 2. Retarget PR2 to main (works because PR2 is still OPEN at this point)
gh pr edit <PR2> --base main
# 3. NOW it's safe to delete PR1's branch
gh api -X DELETE repos/<owner>/<repo>/git/refs/heads/feature-pr1
The retarget rebases PR2's diff against main (which now contains PR1's
squash). PR2 may then need a manual rebase or merge of main to resolve
conflicts, but the PR stays open and reviewable throughout.
After recovery, verify:
# Old PR is closed-unmerged (intentional dead state)
gh pr view <DEAD_PR> --json state,mergedAt
# {"state":"CLOSED","mergedAt":null}
# New PR is open against main with the expected diff
gh pr view <NEW_PR> --json state,baseRefName,headRefName
# {"state":"OPEN","baseRefName":"main","headRefName":"feature-pr2"}
# New PR's diff matches what you intended (no PR1 commits leaked back in)
gh pr diff <NEW_PR> | head -50
gh api -X DELETE
on a refs/heads/X executes immediately and the auto-close cascade is a
GitHub-server-side reaction that lands within ~1 second. There is no
"this will close N dependent PRs, continue?" prompt.gh pr merge <N> --delete-branch
(the normal CLI merge-and-cleanup flow) and gh api -X DELETE refs/heads/<branch>.
Earlier versions of this skill suggested gh pr merge --delete-branch
was safe — that's wrong; it routes through the same Git Refs API
deletion and triggers the cascade. The UI button is the sole exception
because it goes through a different code path that does the retarget
before the delete.gh api -X PUT refs/heads/feature-pr1). The
closed state is sticky.gh-pr-merge-worktree-checkout-trap
documents why operators reach for gh api -X DELETE in the first place
(because gh pr merge --delete-branch fails locally when another worktree
has main checked out). That skill says "the merge succeeded; verify
state=MERGED and move on" — this skill extends that advice with: if you
also need to clean up the branch AND a stacked PR depends on it, follow
Pattern A or B above instead of using the API delete directly.reland-work-that-missed-a-squash-merge
covers commits pushed after PR squash-merge — different failure mode
(stranded commits vs auto-closed PR).Two-PR stack:
base: main, head: worktree-chatbox-with-database: worktree-chatbox-with-data,
head: worktree-chatbox-with-data-pr2Sequence that hit the trap:
36111e42 on main). ✓gh pr merge 456 --squash --delete-branch — local-side failed with
"fatal: 'main' is already used by worktree at '...wow-action'"
(the worktree-checkout-trap; merge succeeded on GitHub, only local
cleanup failed).gh api -X DELETE repos/wan-huiyan/.../git/refs/heads/worktree-chatbox-with-datastate: CLOSED. No notification.gh pr reopen 461 → Could not open the pull request.gh pr edit 461 --base main → Cannot change the base branch of a closed PR.--base main --head worktree-chatbox-with-data-pr2,
reused the body, linked the review-comment thread on the dead #461.
PR #465 squash-merged successfully.Total recovery time: ~5 minutes. Could have been zero with Pattern A (don't delete PR1's branch until PR2 is also merged).
gh pr merge --delete-branch variant)Two-PR stack:
base: main, head: iql-sync/2026-05-18base: iql-sync/2026-05-18,
head: iql-sync/streaming-and-foldersSequence that hit the trap:
gh pr merge 8 --squash --delete-branch. ✓
No worktree conflict; the merge AND the branch deletion both succeeded
cleanly through gh — no API workaround used.state: CLOSED with
mergeStateStatus: DIRTY, mergeable: CONFLICTING.gh pr edit 10 --base main → Cannot change the base branch of a closed PR.gh pr reopen 10 → Could not open the pull request.git rebase origin/main on iql-sync/streaming-and-folders
(git auto-skipped the duplicate squashed commit, leaving just the new
code on top of main), force-push, gh pr create --base main --head iql-sync/streaming-and-folders → PR #11. Merged successfully.Why this case matters: the earlier 2026-05-08 chatbox example
established the trap for the gh api -X DELETE route only. This case
proves the trap fires for gh pr merge --delete-branch too — the
"normal" CLI flow. The skill's trigger conditions were updated in v1.1.0
to reflect this.
Total recovery time: ~3 minutes (no worktree-checkout-trap to untangle first; just rebase + new PR).
gh-pr-merge-worktree-checkout-trap — in this same plugin
(source)What SKILL.md sets; muted values are Claude Code's defaults
npx claudepluginhub wan-huiyan/agent-traffic-control --plugin agent-traffic-control118 skills in this plugin
/agent-refusal-with-evidence-beats-literal-complianceA dispatched agent that does exactly what you asked can ship a defect you would
have stopped yourself, because it can see something at the worksite that you
could not see when you wrote the brief. Use when: (1) you are writing a brief
that prescribes a specific fix, threshold, wording or value rather than the
outcome you want; (2) an agent reports that it did not do what you asked and
you are deciding whether that is insubordination or judgement; (3) an agent
asserts "this is fine" or "not needed" with no measurement behind it; (4) you
are reviewing overnight output and want to know which decisions were checked
and which were assumed. Two halves, and both are needed: give the agent
explicit permission to satisfy the INTENT of an instruction while refusing its
letter, and REQUIRE the evidence with the refusal — what it built or measured,
what that showed, what it did instead. Evidence is the whole difference between
a refusal you can bank and one you have to re-litigate. Four agents correctly
declined an instruction in a single run; one built the suggested fix, measured
that it turned a silent safe refusal into a permanently red pipeline, and
reverted it unpushed.
/auto-merge-rearms-while-agent-live-kill-then-disarm-verifyDisarming auto-merge on a PR whose agent is still running does not hold it —
the agent re-arms within minutes and the PR merges, because arming is a step in
the agent's brief and disarming is not an instruction to the agent at all. Use
when: (1) you want to STOP a pull request from merging that an agent opened and
armed, for review, for a rethink, or because it is landing into a queue you are
trying to drain; (2) a PR you disarmed merged anyway; (3) you are telling agents
which PRs are on hold and want the hold to be enforced rather than announced;
(4) you are about to run `gh pr merge --disable-auto` and have not decided what
to do about the agent. The order is KILL THE AGENT FIRST, THEN DISARM, THEN
VERIFY the disarm by reading `autoMergeRequest` back — a disarm you did not read
back is not a hold. The stronger form needs no ordering at all: convert the PR
to a draft with `gh pr ready --undo`. A draft cannot merge whatever any agent
does to it, so the hold survives an agent you missed, an agent that restarts,
and an agent you did not know about.
/batch-member-cites-a-path-only-a-non-member-addsYou wrote something correct that the default reader cannot reach: a batch member citing a
path only a NON-member adds, or a rule, ruling or ledger sitting on an unlanded branch.
/batch-merge-subject-is-not-evidence-a-member-landedYou are about to close, delete the branch of, or mark as landed a pull
request because a batch, roll-up or merge-train commit on the target branch
names it in the subject line, and because the member's own commits all
predate that merge by wall clock. Neither of those is evidence. A batch
branch is assembled at some moment BEFORE it merges, so anything the member
pushed after assembly is outside it, and the subject records INTENT at
assembly time rather than CONTENT at merge time. Use before closing a
member, when an ancestry check against a squashed batch says no, or when a
member's branch still shows commits ahead of the target. Compare file
content against the target branch instead, expecting exactly one legitimate
difference: the shared aggregate file every member appends to.
/check-a-stale-branch-is-not-undoing-newer-main-workA branch built on an out-of-date main is about to merge, or has merged, and may undo work
that landed since. Use when a green, conflict-free PR deletes files you never touched,
newer values or a whole redesign vanish after a sibling PR merged, rows of a shared ledger
(tracker, registry, CHANGELOG) disappear or revert, or when `git diff main...HEAD` or a
reviewer says your small PR touches dozens of files you did not change.
/ci-leg-skipping-moves-minutes-it-does-not-remove-themYou are about to propose, price or approve a CI cost saving that works by
making a job or matrix leg SKIP on pull requests — a path filter, a safe
list, a changed-files gate, an exemption list. Check first whether the
pipeline runs a complement or backstop on merge, on push, or nightly:
many do, and then the legs a pull request skipped are exactly the legs some
later run executes, so a branch that goes green once and merges bills the
same total either way and the wall clock does not move at all. Use when
pricing a filter change, when a percentage saving is quoted with no basis,
or when choosing between filtering legs and batching pull requests. The
question that separates money from bookkeeping is whether the change
removes RUNS or merely moves LEGS.