Repository navigation
Closed
Description
Context
merge_pull_request (in packages/api/mcp/toolsPullRequests.ts) collapses several different situations into two messages:
PRECONDITION_FAILED: PR must be open and ready for review.— thrown when the PR is closed, already merged, or a draft. The caller cannot tell which, and in the reported incident the PR had in fact already been merged.CHECKS_NOT_PASSED: PR head, required reviews or branch protection requirements are not satisfied.— thrown for a stale head,mergeStateStatusofBEHIND/BLOCKED/DIRTY/UNSTABLE/UNKNOWN,CHANGES_REQUESTED,REVIEW_REQUIRED, or a failing/pending check rollup.
The other PR tools repeat Pull request is not open. with no state. This issue makes every PR precondition failure specific and ties into the error envelope (stage: 'precondition', retryable, details).
Requirements
- New helper
pullRequestSnapshot(pr, graph?)returning the actual state:{ "state": "open" | "closed" | "merged", "draft": false, "merged": true, "mergedAt": "...", "mergeCommitSha": "...", "closedAt": "...", "head": "<sha>", "base": "main", "mergeStateStatus": "BLOCKED", "reviewDecision": "REVIEW_REQUIRED", "checks": { "state": "PENDING", "failing": ["build"], "pending": ["e2e"] }, "url": "..." } - New helper
assertPullRequestOpen(pr, action)used by all PR mutation tools, throwing one of:PULL_REQUEST_ALREADY_MERGED(409, not retryable, details includemergedAt,mergeCommitSha) — formerge_pull_requestthis is reported as such so the agent can tell the user "it was already merged".PULL_REQUEST_CLOSED(409, not retryable,closedAt).PULL_REQUEST_DRAFT(409, not retryable) — only for merge (other commands can run on drafts as today? keep current behaviour: other commands only require open).
merge_pull_requestevaluates preconditions in order and reports the first failing one, always attachingdetails.currentState(the snapshot) anddetails.failedPrecondition:Precondition Code Retryable head equals expectedHeadSTALE_HEADfalse not draft PULL_REQUEST_DRAFTfalse reviewDecision !== 'CHANGES_REQUESTED'CHANGES_REQUESTEDfalse reviewDecision !== 'REVIEW_REQUIRED'REVIEW_REQUIREDfalse check rollup not FAILURE/ERRORCHECKS_FAILING(details.failing = names)false check rollup not PENDING/EXPECTEDCHECKS_PENDING(details.pending = names)true mergeStateStatus === 'BEHIND'BRANCH_BEHIND_BASE(message suggestsupdate_pull_request_branch)false mergeStateStatus === 'DIRTY'MERGE_CONFLICTfalse mergeStateStatus === 'BLOCKED'BRANCH_PROTECTION_BLOCKEDfalse mergeStateStatus === 'UNKNOWN'MERGE_STATE_UNKNOWN(GitHub still computing)true mergeStateStatus === 'UNSTABLE'/HAS_HOOKS→ allowed only if rollup SUCCESSCHECKS_FAILINGfalse GitHub merge API returns merged: falseor 405/409MERGE_REJECTEDwith GitHub's messagefalse - The GraphQL query fetches check names:
statusCheckRollup { state contexts(first: 50) { nodes { ... on CheckRun { name conclusion status } ... on StatusContext { context state } } } }, plusisDraft,merged,mergedAt,closedAt,state. - Messages are human sentences naming the precondition, e.g. "Cannot merge AI Fix for Issue #40: Feat: Monitor PR comments for follow-up tasks #42: 2 required checks are still running (e2e, lint). Try again when they finish."
- Replace every
'Pull request is not open.'/'Pull request must be open.'throw intoolsPullRequests.tswithassertPullRequestOpen.
Implementation Specification
- New
packages/api/mcp/pullRequestPreconditions.ts(snapshot + assertions + merge evaluation, pure functions, no GitHub calls). - Edit
packages/api/mcp/toolsPullRequests.ts(merge_pull_requestand the open checks). - Tests
packages/api/test/mcpMergePreconditions.test.tscovering every row of the table (pure-function tests) and one integration case throughexecuteTool.
Acceptance Criteria
- Merging an already-merged PR fails with
PULL_REQUEST_ALREADY_MERGED,details.currentState.mergedAtset. - Each row of the table has a unit test asserting code, retryable flag and that
details.currentStateis present. -
comment_on_pull_requeston a closed PR fails withPULL_REQUEST_CLOSED(notPRECONDITION_FAILED). - The receipt of a failed merge (
get_operation) shows the same envelope. - No behaviour change for a clean, approved, passing PR (merge succeeds exactly as before).
Created by ProPR AI Planner
Activity
propr-dev commentedon Sep 28, 2026
Suggested Implementation:
Implementation Approach
We separate evaluation (pure: snapshot + ordered rule list) from effects (GitHub reads and the merge call). Pure functions make every precondition row unit-testable without a GitHub fake and keep the order explicit, so the "first failing precondition" is deterministic. We report precondition failures via
McpErrorwithstage: 'precondition'so the envelope from the first issue carries them unchanged into receipts.We deliberately keep GitHub's merge API as the final authority (it re-checks head SHA and protection atomically). Our pre-check exists to explain why before attempting, not to replace GitHub's enforcement.
File:
packages/api/mcp/pullRequestPreconditions.ts(new file)Why this file: Holds the snapshot shape and precondition rules shared by merge and all PR mutation tools.
File:
packages/api/mcp/toolsPullRequests.tsWhy this file:
merge_pull_requestand the open checks of the other PR tools live here.File:
packages/api/test/mcpMergePreconditions.test.ts(new file)Why this file: Table-driven proof for every precondition code, retryability and attached state.
propr-dev commentedon Sep 29, 2026
🤖 AI processing has started for this issue using codex agent with gpt-5.6-sol model.
I'll analyze the problem and work on a solution. This may take a few minutes.
Processing Details:
codexgpt-5.6-sol2600/gpt-5.6-sol-mcp-merge_pull_request-an-20260929-1746-4ds2593-epic-mcp-observability-cf6issue-2600-20260929-1746-gpt-5.6-sol-4ds🔍 Track Task Execution
fix(ai): Resolve issue #2600 - MCP merge_pull_request and PR state ch…
fix(ai): Resolve issue #2600 - MCP merge_pull_request and PR state ch…