Skip to content

Self-hosted, Self-Improving, Federated Everything App — dev management, agent orchestration, persistent mind, digital twin, second brain, and creative pipeline from a single dashboard over Tailscale to port the user into the OS

fix: recognize merged PRs despite CLI cleanup failure (#9837)

Adam Eivyd00fdda8b431
diff --git a/server/services/agentPromptBuilder.test.js b/server/services/agentPromptBuilder.test.js
index 8acbd883f5..b6e1b00e59 100644
--- a/server/services/agentPromptBuilder.test.js
+++ b/server/services/agentPromptBuilder.test.js
@@ -916,6 +916,9 @@ describe('buildLightContextPrompt', () => {
expect(prompt).toMatch(/gh pr merge "<PR_URL>" --merge --delete-branch/);
expect(prompt).not.toMatch(/gh pr merge[^\n]*--auto/);
expect(prompt).toMatch(/gh pr view "<PR_URL>" --json state -q \.state/);
+ expect(prompt).toContain('If it returns `MERGED`, publication succeeded even when the CLI failed during local cleanup');
+ expect(prompt).toContain('Continue to the completion sentinel when this workflow requires one');
+ expect(prompt).toContain('Only while the PR is still unmerged');
// ...and the gate is CI, not a review-loop status.
expect(prompt).not.toMatch(/review loop reports/);
});
@@ -1530,7 +1533,10 @@ describe('buildLightContextPrompt', () => {
// 40KB recipe itself must still stay in the staged file — which is the
// only thing this ceiling is measuring, so it carries growth headroom
// rather than pinning today's byte count.
- expect(prompt.replace(MANDATORY_DISPATCH_HINT_GUIDANCE, '').length).toBeLessThan(27_000);
+ // #9837 adds remote-state-first recovery and managed-cleanup ownership.
+ // Keep bounded headroom for that safety prose while the explicit recipe
+ // omission above and this ceiling still reject inlining the 40KB recipe.
+ expect(prompt.replace(MANDATORY_DISPATCH_HINT_GUIDANCE, '').length).toBeLessThan(29_000);
});
it('quotes a hostile branch ref inert in the PR-create command line', () => {
diff --git a/server/services/promptSections/__snapshots__/reviewLoopMatrix/followUp-gh-merge-compact.txt b/server/services/promptSections/__snapshots__/reviewLoopMatrix/followUp-gh-merge-compact.txt
index bff4b7d342..8b42561ff4 100644
--- a/server/services/promptSections/__snapshots__/reviewLoopMatrix/followUp-gh-merge-compact.txt
+++ b/server/services/promptSections/__snapshots__/reviewLoopMatrix/followUp-gh-merge-compact.txt
@@ -31,7 +31,8 @@ Only an `ok: true` response with a validated `clean` or `findings` verdict and a
gh pr merge "https://github.com/example-org/example-repo/pull/42" --merge --delete-branch
```
(Equivalent: `gh pr merge 42 --repo example-org/example-repo --merge --delete-branch`.)
- If that is rejected because this repo disallows merge commits, re-check what it allows (`gh repo view --json mergeCommitAllowed,squashMergeAllowed,rebaseMergeAllowed`) and merge with an allowed method instead — `--squash` first, else `--rebase`, keeping `--delete-branch`.
+ **Merge-command failure recovery:** If the CLI exits nonzero, first read the remote PR state with `gh pr view "https://github.com/example-org/example-repo/pull/42" --json state -q .state`. If it returns `MERGED`, publication succeeded even when the CLI failed during local cleanup (for example, because the primary checkout already owns the default branch). Do not retry a successful merge. Leave the active managed local worktree and its branch to PortOS cleanup; never checkout the default branch to satisfy CLI cleanup, remove another worktree, alter its registration, or discard dirty work. Continue to the completion sentinel when this workflow requires one. Verify deletion of this merged task's exact remote head branch (read `gh pr view "https://github.com/example-org/example-repo/pull/42" --json headRefName -q .headRefName`, then query that exact `refs/heads/<headRefName>` on its verified remote with `git ls-remote --heads`); a failed lookup is not proof of absence. If it still exists, retry remote deletion only for that verified task branch with `git push <verified-remote> --delete <headRefName>`, never another branch or a local worktree. Only while the PR is still unmerged may you investigate merge blockers or try an allowed merge-method fallback; a failed state lookup is inconclusive, never success.
+ If the PR is still unmerged and the command was rejected because this repo disallows merge commits, re-check what it allows (`gh repo view --json mergeCommitAllowed,squashMergeAllowed,rebaseMergeAllowed`) and merge with an allowed method instead — `--squash` first, else `--rebase`, keeping `--delete-branch`.
7. **Confirm the merge before exiting**: `gh pr view "https://github.com/example-org/example-repo/pull/42" --json state -q .state` must return `MERGED`. If it is still open or was closed unmerged, investigate (failing check, merge conflict, branch protection), fix, and retry. Leave it open — saying so explicitly in your completion summary — if CI stays red after a genuine fix attempt, a conflict needs a human decision, expected checks never attached, or a branch protection you cannot satisfy blocks the merge (a required approving review, a required check only a human can trigger). Hand those to a human rather than retrying until you time out.
8. Exit. Do **not** run `/do:push` or open a new PR — the merge handles everything. The system will clean up your worktree on exit.
**Hard stop:** if a required reviewer's loop is not converged after 10 rounds, post a PR comment summarising blockers and exit. An optional reviewer may end with an inconclusive result without blocking, but a substantive rejection, failed build/test, or push failure still blocks.
diff --git a/server/services/promptSections/__snapshots__/reviewLoopMatrix/followUp-gh-merge-verbose.txt b/server/services/promptSections/__snapshots__/reviewLoopMatrix/followUp-gh-merge-verbose.txt
index dd6984145e..f125d4d82f 100644
--- a/server/services/promptSections/__snapshots__/reviewLoopMatrix/followUp-gh-merge-verbose.txt
+++ b/server/services/promptSections/__snapshots__/reviewLoopMatrix/followUp-gh-merge-verbose.txt
@@ -36,7 +36,8 @@ Only an `ok: true` response with a validated `clean` or `findings` verdict and a
gh pr merge "https://github.com/example-org/example-repo/pull/42" --merge --delete-branch
```
(Equivalent: `gh pr merge 42 --repo example-org/example-repo --merge --delete-branch`.)
- If that is rejected because this repo disallows merge commits, re-check what it allows (`gh repo view --json mergeCommitAllowed,squashMergeAllowed,rebaseMergeAllowed`) and merge with an allowed method instead — `--squash` first, else `--rebase`, keeping `--delete-branch`.
+ **Merge-command failure recovery:** If the CLI exits nonzero, first read the remote PR state with `gh pr view "https://github.com/example-org/example-repo/pull/42" --json state -q .state`. If it returns `MERGED`, publication succeeded even when the CLI failed during local cleanup (for example, because the primary checkout already owns the default branch). Do not retry a successful merge. Leave the active managed local worktree and its branch to PortOS cleanup; never checkout the default branch to satisfy CLI cleanup, remove another worktree, alter its registration, or discard dirty work. Continue to the completion sentinel when this workflow requires one. Verify deletion of this merged task's exact remote head branch (read `gh pr view "https://github.com/example-org/example-repo/pull/42" --json headRefName -q .headRefName`, then query that exact `refs/heads/<headRefName>` on its verified remote with `git ls-remote --heads`); a failed lookup is not proof of absence. If it still exists, retry remote deletion only for that verified task branch with `git push <verified-remote> --delete <headRefName>`, never another branch or a local worktree. Only while the PR is still unmerged may you investigate merge blockers or try an allowed merge-method fallback; a failed state lookup is inconclusive, never success.
+ If the PR is still unmerged and the command was rejected because this repo disallows merge commits, re-check what it allows (`gh repo view --json mergeCommitAllowed,squashMergeAllowed,rebaseMergeAllowed`) and merge with an allowed method instead — `--squash` first, else `--rebase`, keeping `--delete-branch`.
7. **Confirm the merge before exiting**: `gh pr view "https://github.com/example-org/example-repo/pull/42" --json state -q .state` must return `MERGED`. If it is still open or was closed unmerged, investigate (failing check, merge conflict, branch protection), fix, and retry. Leave it open — saying so explicitly in your completion summary — if CI stays red after a genuine fix attempt, a conflict needs a human decision, expected checks never attached, or a branch protection you cannot satisfy blocks the merge (a required approving review, a required check only a human can trigger). Hand those to a human rather than retrying until you time out.
8. Exit. Do **not** run `/do:push` or open a new PR — the merge handles everything. The system will clean up your worktree on exit.
diff --git a/server/services/promptSections/__snapshots__/reviewLoopMatrix/followup-copilot-first.txt b/server/services/promptSections/__snapshots__/reviewLoopMatrix/followup-copilot-first.txt
index 0e5d7c4b65..64ec108e59 100644
--- a/server/services/promptSections/__snapshots__/reviewLoopMatrix/followup-copilot-first.txt
+++ b/server/services/promptSections/__snapshots__/reviewLoopMatrix/followup-copilot-first.txt
@@ -19,7 +19,8 @@ A previous agent finished task **task-example** and opened **PR https://github.c
gh pr merge "https://github.com/example-org/example-repo/pull/42" --merge --delete-branch
```
(Equivalent: `gh pr merge 42 --repo example-org/example-repo --merge --delete-branch`.)
- If that is rejected because this repo disallows merge commits, re-check what it allows (`gh repo view --json mergeCommitAllowed,squashMergeAllowed,rebaseMergeAllowed`) and merge with an allowed method instead — `--squash` first, else `--rebase`, keeping `--delete-branch`.
+ **Merge-command failure recovery:** If the CLI exits nonzero, first read the remote PR state with `gh pr view "https://github.com/example-org/example-repo/pull/42" --json state -q .state`. If it returns `MERGED`, publication succeeded even when the CLI failed during local cleanup (for example, because the primary checkout already owns the default branch). Do not retry a successful merge. Leave the active managed local worktree and its branch to PortOS cleanup; never checkout the default branch to satisfy CLI cleanup, remove another worktree, alter its registration, or discard dirty work. Continue to the completion sentinel when this workflow requires one. Verify deletion of this merged task's exact remote head branch (read `gh pr view "https://github.com/example-org/example-repo/pull/42" --json headRefName -q .headRefName`, then query that exact `refs/heads/<headRefName>` on its verified remote with `git ls-remote --heads`); a failed lookup is not proof of absence. If it still exists, retry remote deletion only for that verified task branch with `git push <verified-remote> --delete <headRefName>`, never another branch or a local worktree. Only while the PR is still unmerged may you investigate merge blockers or try an allowed merge-method fallback; a failed state lookup is inconclusive, never success.
+ If the PR is still unmerged and the command was rejected because this repo disallows merge commits, re-check what it allows (`gh repo view --json mergeCommitAllowed,squashMergeAllowed,rebaseMergeAllowed`) and merge with an allowed method instead — `--squash` first, else `--rebase`, keeping `--delete-branch`.
7. **Confirm the merge before exiting**: `gh pr view "https://github.com/example-org/example-repo/pull/42" --json state -q .state` must return `MERGED`. If it is still open or was closed unmerged, investigate (failing check, merge conflict, branch protection), fix, and retry. Leave it open — saying so explicitly in your completion summary — if CI stays red after a genuine fix attempt, a conflict needs a human decision, expected checks never attached, or a branch protection you cannot satisfy blocks the merge (a required approving review, a required check only a human can trigger). Hand those to a human rather than retrying until you time out.
8. Exit. Do **not** run `/do:push` or open a new PR — the merge handles everything. The system will clean up your worktree on exit.
**Hard stop:** if a reviewer's loop is not converged after 10 rounds, post a PR comment summarising blockers and exit.
diff --git a/server/services/promptSections/__snapshots__/reviewLoopMatrix/inline-gh-merge-compact.txt b/server/services/promptSections/__snapshots__/reviewLoopMatrix/inline-gh-merge-compact.txt
index c8e95696a6..573140e8ae 100644
--- a/server/services/promptSections/__snapshots__/reviewLoopMatrix/inline-gh-merge-compact.txt
+++ b/server/services/promptSections/__snapshots__/reviewLoopMatrix/inline-gh-merge-compact.txt
@@ -41,7 +41,8 @@ Only an `ok: true` response with a validated `clean` or `findings` verdict and a
gh pr merge "https://github.com/example-org/example-repo/pull/42" --merge --delete-branch
```
(Equivalent: `gh pr merge 42 --repo example-org/example-repo --merge --delete-branch`.)
- If that is rejected because this repo disallows merge commits, re-check what it allows (`gh repo view --json mergeCommitAllowed,squashMergeAllowed,rebaseMergeAllowed`) and merge with an allowed method instead — `--squash` first, else `--rebase`, keeping `--delete-branch`.
+ **Merge-command failure recovery:** If the CLI exits nonzero, first read the remote PR state with `gh pr view "https://github.com/example-org/example-repo/pull/42" --json state -q .state`. If it returns `MERGED`, publication succeeded even when the CLI failed during local cleanup (for example, because the primary checkout already owns the default branch). Do not retry a successful merge. Leave the active managed local worktree and its branch to PortOS cleanup; never checkout the default branch to satisfy CLI cleanup, remove another worktree, alter its registration, or discard dirty work. Continue to the completion sentinel when this workflow requires one. Verify deletion of this merged task's exact remote head branch (read `gh pr view "https://github.com/example-org/example-repo/pull/42" --json headRefName -q .headRefName`, then query that exact `refs/heads/<headRefName>` on its verified remote with `git ls-remote --heads`); a failed lookup is not proof of absence. If it still exists, retry remote deletion only for that verified task branch with `git push <verified-remote> --delete <headRefName>`, never another branch or a local worktree. Only while the PR is still unmerged may you investigate merge blockers or try an allowed merge-method fallback; a failed state lookup is inconclusive, never success.
+ If the PR is still unmerged and the command was rejected because this repo disallows merge commits, re-check what it allows (`gh repo view --json mergeCommitAllowed,squashMergeAllowed,rebaseMergeAllowed`) and merge with an allowed method instead — `--squash` first, else `--rebase`, keeping `--delete-branch`.
7. **Confirm the merge before exiting**: `gh pr view "https://github.com/example-org/example-repo/pull/42" --json state -q .state` must return `MERGED`. If it is still open or was closed unmerged, investigate (failing check, merge conflict, branch protection), fix, and retry. Leave it open — saying so explicitly in your completion summary — if CI stays red after a genuine fix attempt, a conflict needs a human decision, expected checks never attached, or a branch protection you cannot satisfy blocks the merge (a required approving review, a required check only a human can trigger). Hand those to a human rather than retrying until you time out.
8. Return to the **Completion Workflow** above and write the completion sentinel — the run is not done until you have. Do NOT open a second PR.
**Hard stop:** if a required reviewer's loop is not converged after 10 rounds, post a PR comment summarising blockers and exit. An optional reviewer may end with an inconclusive result without blocking, but a substantive rejection, failed build/test, or push failure still blocks.
diff --git a/server/services/promptSections/__snapshots__/reviewLoopMatrix/inline-gh-merge-verbose.txt b/server/services/promptSections/__snapshots__/reviewLoopMatrix/inline-gh-merge-verbose.txt
index beaaf70cf8..275fd9a263 100644
--- a/server/services/promptSections/__snapshots__/reviewLoopMatrix/inline-gh-merge-verbose.txt
+++ b/server/services/promptSections/__snapshots__/reviewLoopMatrix/inline-gh-merge-verbose.txt
@@ -46,7 +46,8 @@ Only an `ok: true` response with a validated `clean` or `findings` verdict and a
gh pr merge "https://github.com/example-org/example-repo/pull/42" --merge --delete-branch
```
(Equivalent: `gh pr merge 42 --repo example-org/example-repo --merge --delete-branch`.)
- If that is rejected because this repo disallows merge commits, re-check what it allows (`gh repo view --json mergeCommitAllowed,squashMergeAllowed,rebaseMergeAllowed`) and merge with an allowed method instead — `--squash` first, else `--rebase`, keeping `--delete-branch`.
+ **Merge-command failure recovery:** If the CLI exits nonzero, first read the remote PR state with `gh pr view "https://github.com/example-org/example-repo/pull/42" --json state -q .state`. If it returns `MERGED`, publication succeeded even when the CLI failed during local cleanup (for example, because the primary checkout already owns the default branch). Do not retry a successful merge. Leave the active managed local worktree and its branch to PortOS cleanup; never checkout the default branch to satisfy CLI cleanup, remove another worktree, alter its registration, or discard dirty work. Continue to the completion sentinel when this workflow requires one. Verify deletion of this merged task's exact remote head branch (read `gh pr view "https://github.com/example-org/example-repo/pull/42" --json headRefName -q .headRefName`, then query that exact `refs/heads/<headRefName>` on its verified remote with `git ls-remote --heads`); a failed lookup is not proof of absence. If it still exists, retry remote deletion only for that verified task branch with `git push <verified-remote> --delete <headRefName>`, never another branch or a local worktree. Only while the PR is still unmerged may you investigate merge blockers or try an allowed merge-method fallback; a failed state lookup is inconclusive, never success.
+ If the PR is still unmerged and the command was rejected because this repo disallows merge commits, re-check what it allows (`gh repo view --json mergeCommitAllowed,squashMergeAllowed,rebaseMergeAllowed`) and merge with an allowed method instead — `--squash` first, else `--rebase`, keeping `--delete-branch`.
7. **Confirm the merge before exiting**: `gh pr view "https://github.com/example-org/example-repo/pull/42" --json state -q .state` must return `MERGED`. If it is still open or was closed unmerged, investigate (failing check, merge conflict, branch protection), fix, and retry. Leave it open — saying so explicitly in your completion summary — if CI stays red after a genuine fix attempt, a conflict needs a human decision, expected checks never attached, or a branch protection you cannot satisfy blocks the merge (a required approving review, a required check only a human can trigger). Hand those to a human rather than retrying until you time out.
8. Return to the **Completion Workflow** above and write the completion sentinel — the run is not done until you have. Do NOT open a second PR.
diff --git a/server/services/promptSections/__snapshots__/reviewLoopMatrix/mergeonly-followUp-gh.txt b/server/services/promptSections/__snapshots__/reviewLoopMatrix/mergeonly-followUp-gh.txt
index 2b867f04cb..e5ab3df65f 100644
--- a/server/services/promptSections/__snapshots__/reviewLoopMatrix/mergeonly-followUp-gh.txt
+++ b/server/services/promptSections/__snapshots__/reviewLoopMatrix/mergeonly-followUp-gh.txt
@@ -7,7 +7,8 @@ A previous agent finished the work for source task **task-example** and opened *
```bash
gh pr merge "https://github.com/example-org/example-repo/pull/42" --merge --delete-branch
```
- If that is rejected because this repo disallows merge commits, re-check what it allows (`gh repo view --json mergeCommitAllowed,squashMergeAllowed,rebaseMergeAllowed`) and merge with an allowed method instead — `--squash` first, else `--rebase`, keeping `--delete-branch`.
+ **Merge-command failure recovery:** If the CLI exits nonzero, first read the remote PR state with `gh pr view "https://github.com/example-org/example-repo/pull/42" --json state -q .state`. If it returns `MERGED`, publication succeeded even when the CLI failed during local cleanup (for example, because the primary checkout already owns the default branch). Do not retry a successful merge. Leave the active managed local worktree and its branch to PortOS cleanup; never checkout the default branch to satisfy CLI cleanup, remove another worktree, alter its registration, or discard dirty work. Continue to the completion sentinel when this workflow requires one. Verify deletion of this merged task's exact remote head branch (read `gh pr view "https://github.com/example-org/example-repo/pull/42" --json headRefName -q .headRefName`, then query that exact `refs/heads/<headRefName>` on its verified remote with `git ls-remote --heads`); a failed lookup is not proof of absence. If it still exists, retry remote deletion only for that verified task branch with `git push <verified-remote> --delete <headRefName>`, never another branch or a local worktree. Only while the PR is still unmerged may you investigate merge blockers or try an allowed merge-method fallback; a failed state lookup is inconclusive, never success.
+ If the PR is still unmerged and the command was rejected because this repo disallows merge commits, re-check what it allows (`gh repo view --json mergeCommitAllowed,squashMergeAllowed,rebaseMergeAllowed`) and merge with an allowed method instead — `--squash` first, else `--rebase`, keeping `--delete-branch`.
4. **Confirm the merge before exiting**: `gh pr view "https://github.com/example-org/example-repo/pull/42" --json state -q .state` must return `MERGED`. If it is still open or was closed unmerged, investigate (failing check, merge conflict, branch protection), fix, and retry. Leave it open — saying so explicitly in your completion summary — if CI stays red after a genuine fix attempt, a conflict needs a human decision, expected checks never attached, or a branch protection you cannot satisfy blocks the merge (a required approving review, a required check only a human can trigger). Hand those to a human rather than retrying until you time out.
5. Exit. Do NOT run `/do:push`, do NOT open a new PR, and do NOT start a code review — landing this PR is the whole job.
diff --git a/server/services/promptSections/__snapshots__/reviewLoopMatrix/mergeonly-inline-gh.txt b/server/services/promptSections/__snapshots__/reviewLoopMatrix/mergeonly-inline-gh.txt
index edb3f3b158..a239d7ddf9 100644
--- a/server/services/promptSections/__snapshots__/reviewLoopMatrix/mergeonly-inline-gh.txt
+++ b/server/services/promptSections/__snapshots__/reviewLoopMatrix/mergeonly-inline-gh.txt
@@ -7,7 +7,8 @@ This runs as **step 4 of the Completion Workflow above**, against the PR you jus
```bash
gh pr merge "https://github.com/example-org/example-repo/pull/42" --merge --delete-branch
```
- If that is rejected because this repo disallows merge commits, re-check what it allows (`gh repo view --json mergeCommitAllowed,squashMergeAllowed,rebaseMergeAllowed`) and merge with an allowed method instead — `--squash` first, else `--rebase`, keeping `--delete-branch`.
+ **Merge-command failure recovery:** If the CLI exits nonzero, first read the remote PR state with `gh pr view "https://github.com/example-org/example-repo/pull/42" --json state -q .state`. If it returns `MERGED`, publication succeeded even when the CLI failed during local cleanup (for example, because the primary checkout already owns the default branch). Do not retry a successful merge. Leave the active managed local worktree and its branch to PortOS cleanup; never checkout the default branch to satisfy CLI cleanup, remove another worktree, alter its registration, or discard dirty work. Continue to the completion sentinel when this workflow requires one. Verify deletion of this merged task's exact remote head branch (read `gh pr view "https://github.com/example-org/example-repo/pull/42" --json headRefName -q .headRefName`, then query that exact `refs/heads/<headRefName>` on its verified remote with `git ls-remote --heads`); a failed lookup is not proof of absence. If it still exists, retry remote deletion only for that verified task branch with `git push <verified-remote> --delete <headRefName>`, never another branch or a local worktree. Only while the PR is still unmerged may you investigate merge blockers or try an allowed merge-method fallback; a failed state lookup is inconclusive, never success.
+ If the PR is still unmerged and the command was rejected because this repo disallows merge commits, re-check what it allows (`gh repo view --json mergeCommitAllowed,squashMergeAllowed,rebaseMergeAllowed`) and merge with an allowed method instead — `--squash` first, else `--rebase`, keeping `--delete-branch`.
4. **Confirm the merge before exiting**: `gh pr view "https://github.com/example-org/example-repo/pull/42" --json state -q .state` must return `MERGED`. If it is still open or was closed unmerged, investigate (failing check, merge conflict, branch protection), fix, and retry. Leave it open — saying so explicitly in your completion summary — if CI stays red after a genuine fix attempt, a conflict needs a human decision, expected checks never attached, or a branch protection you cannot satisfy blocks the merge (a required approving review, a required check only a human can trigger). Hand those to a human rather than retrying until you time out.
5. Return to the **Completion Workflow** above and write the completion sentinel — the run is not done until you have. Do NOT open a second PR. Do NOT start a code review — none is configured for this task.
diff --git a/server/services/promptSections/reviewLifecycle.js b/server/services/promptSections/reviewLifecycle.js
index 13ba926c2e..cf9f3ffc87 100644
--- a/server/services/promptSections/reviewLifecycle.js
+++ b/server/services/promptSections/reviewLifecycle.js
@@ -1134,7 +1134,8 @@ export function buildCiMergeGateSteps(startStep, { prRef, mrRef = '<MR_NUMBER>',
mergeEquivalentLine,
// Not every repo allows merge commits; a repo restricted to squash/rebase
// rejects `--merge` outright, which would leave the PR open forever.
- gh ? ` If that is rejected because this repo disallows merge commits, re-check what it allows (\`gh repo view --json mergeCommitAllowed,squashMergeAllowed,rebaseMergeAllowed\`) and merge with an allowed method instead — \`--squash\` first, else \`--rebase\`${deleteBranch ? ', keeping \`--delete-branch\`' : ''}.` : null,
+ gh ? ` **Merge-command failure recovery:** If the CLI exits nonzero, first read the remote PR state with \`gh pr view ${prRef} --json state -q .state\`. If it returns \`MERGED\`, publication succeeded even when the CLI failed during local cleanup (for example, because the primary checkout already owns the default branch). Do not retry a successful merge. Leave the active managed local worktree and its branch to PortOS cleanup; never checkout the default branch to satisfy CLI cleanup, remove another worktree, alter its registration, or discard dirty work. Continue to the completion sentinel when this workflow requires one.${deleteBranch ? ` Verify deletion of this merged task's exact remote head branch (read \`gh pr view ${prRef} --json headRefName -q .headRefName\`, then query that exact \`refs/heads/<headRefName>\` on its verified remote with \`git ls-remote --heads\`); a failed lookup is not proof of absence. If it still exists, retry remote deletion only for that verified task branch with \`git push <verified-remote> --delete <headRefName>\`, never another branch or a local worktree.` : ''} Only while the PR is still unmerged may you investigate merge blockers or try an allowed merge-method fallback; a failed state lookup is inconclusive, never success.` : null,
+ gh ? ` If the PR is still unmerged and the command was rejected because this repo disallows merge commits, re-check what it allows (\`gh repo view --json mergeCommitAllowed,squashMergeAllowed,rebaseMergeAllowed\`) and merge with an allowed method instead — \`--squash\` first, else \`--rebase\`${deleteBranch ? ', keeping \`--delete-branch\`' : ''}.` : null,
`${startStep + 3}. **Confirm the merge before exiting**: ${stateCmd}. If it is still open or was closed unmerged, investigate (failing check, merge conflict, branch protection), fix, and retry. Leave it open — saying so explicitly in your completion summary — if CI stays red after a genuine fix attempt, a conflict needs a human decision, expected checks never attached, or a branch protection you cannot satisfy blocks the merge (a required approving review, a required check only a human can trigger). Hand those to a human rather than retrying until you time out.`,
].filter(Boolean);
return { lines, nextStep: startStep + 4 };
diff --git a/server/services/promptSections/reviewLifecycleMatrix.test.js b/server/services/promptSections/reviewLifecycleMatrix.test.js
index ee0e44cef4..f4b9ca2bc5 100644
--- a/server/services/promptSections/reviewLifecycleMatrix.test.js
+++ b/server/services/promptSections/reviewLifecycleMatrix.test.js
@@ -272,6 +272,14 @@ describe('buildReviewLoopFollowUpSection — load-bearing lines stay with their
expect(section).toContain('gh pr merge "https://github.com/example-org/example-repo/pull/42" --merge --delete-branch');
expect(section).toContain('(Equivalent: `gh pr merge 42 --repo example-org/example-repo --merge --delete-branch`.)');
expect(section).toContain('`gh pr view "https://github.com/example-org/example-repo/pull/42" --json state -q .state` must return `MERGED`');
+ expect(section).toContain('If it returns `MERGED`, publication succeeded even when the CLI failed during local cleanup');
+ expect(section).toContain('Do not retry a successful merge');
+ expect(section).toContain('Leave the active managed local worktree and its branch to PortOS cleanup');
+ expect(section).toContain('never checkout the default branch');
+ expect(section).toContain('a failed lookup is not proof of absence');
+ expect(section).toContain('retry remote deletion only for that verified task branch');
+ expect(section).toContain('Only while the PR is still unmerged');
+ expect(section).toContain('a failed state lookup is inconclusive, never success');
expect(section).toContain('8. Exit. Do **not** run `/do:push` or open a new PR — the merge handles everything.');
expect(section).not.toContain('**Cross-phase stop-mode gate:**');
expect(section).not.toContain('Do NOT push or open a PR/MR');