Skip to content

fix: check mergeability before the approval - #1720

Merged
neuromechanist merged 19 commits into
devfrom
fix/pr-reviews-merge-precheck
Oct 12, 2026
Merged

neuromechanist merged 19 commits into
devfrom
fix/pr-reviews-merge-precheck

Conversation

@neuromechanist

@neuromechanist neuromechanist commented Oct 12, 2026 •

Copy link
Copy Markdown
Contributor

Refs #1707 (decision 3 of "Decisions for the maintainer"). Follows #1705, which recorded this as "Recorded, not changed".

Problem

nemar admin pr-reviews approve --merge and y in nemar admin pr-reviews next approved a pull request and only then read GitHub's mergeable_state. A pull request the automated review holds has a check that is not required and not passing, which GitHub is expected to report as unstable, so the merge was refused after the approval had been written. The administrator was left with an approval they asked for only as the first half of a pair. ADR 0093 says a merge is attempted only when GitHub reports clean; it did not say when that is read.

Cause, from the code and confirmed by running the new tests against dev before the fix:

  • approve --merge: nothing read the state before submitApproval. The only check was inside mergeWhenClean, which runs after the approval.
  • next: approveAllowed refused dirty and behind and let every other state through, so y was offered for unstable, blocked, unknown, has_hooks and a missing state.

On dev the stand-in run showed Approved nm000201 #7 ... followed by Not merged: ... The approval stands. and Done: 1 approved but not merged.

What changed

ADR 0093's "clean only" rule is not changed. What moves is where it is checked.

  • approve --merge reads the state first (asking again for a few seconds while it is unknown) and, unless it is clean, writes nothing: no approval, no merge. It says which state GitHub reported or which read failed, why that blocks, and what to do: for a state that will not clear by itself, run approve without --merge; for unknown or a failed read, run the same command again in a moment; for a head that changed since it was read, run it again to read the new one. In next the advice is that s skips the card and a fresh next run looks again (a card is pinned to the head it was shown for, and y when it is not offered does not look again). --dry-run --merge gives the same answer. This happens before the confirmation question.
  • The state is read again right before the approval is written, by one helper (recheckBeforeApproval) that both approve --merge and y call, because the early check can be as old as the confirmation prompt or the time a card was on screen and an approval cannot be taken back. One extra GET when clean; a refusal says it came from that second look. It is taken after the confirmation (and in next when y is answered, not when the card is shown), which tests pin. GitHub has no approve-and-merge call, so this narrows the window to one request and does not close it; the look after the approval still catches a check that starts in between.
  • y in next is offered only when both required checks are green and the state is clean, settled the same way before the card is shown. The card names the state or the failed read and the approve-without-merging command; typing y anyway is refused with the reason and writes nothing. --force does not reach it.
  • Not clean means anything but clean: unstable, blocked, behind, dirty, draft, has_hooks, a state nobody has heard of, a missing state, a state still unknown after the re-asks, a re-read that fails, finds the branch moved, or finds the pull request closed or merged.
  • One value, MergeCheck (ok, the sentence, a card phrase, the advice), made by checkMergeable and mergeCheckOf, is what every caller uses, so the card says what actually happened instead of "GitHub has not said". settleMergeState takes the pull request as read (head and state in one value; a state not yet read is null, not an absent field), and a not-merged result carries the same MergeRefused instead of a loose state string. A state named like an object property (constructor) no longer leaks into the sentence.
  • approve without --merge is unchanged (same verdict gate, same --force). When GitHub's state is one that will not clear by itself, its closing hint says so instead of suggesting --merge.
  • The look after the approval stays, for a state that changes in between. It can still leave an approval standing, but that is now the exception.
  • Wording: unstable is described as a check or status that is not required and is not passing or has not reported, and a held review producing it is called expected, not observed.
  • ADR 0093 amended (and its bullet on the legacy repositories corrected: they have classic branch protection, not a ruleset), ADR 0092's open item marked settled in part, ADR index line updated, CHANGELOG Unreleased entry.

Cost of the second look: one more request to the GitHub REST API per approve --merge or y when the state is clean (three reads of the pull request in all instead of two: the first read, the look before the approval, the look before the merge). An approval alone costs nothing extra.

Test evidence

Tests were written first and run on dev code: 6 of the new approve tests failed there, and 7 of the new or changed next and approveAllowed tests. The review round's tests were again written before the change they cover and failed first (the late look, the failure reasons, the closed pull request, the property-named state). The ones that guard what must keep working (a clean pull request still approved and merged, an unknown that settles still merging, approve alone with --force) passed on both, as intended.

  • Each test file was run on its own: admin-pr-reviews-cli 138 pass, pr-review-approve 72, pr-review-next.unit 17, pr-reviews-render.unit 13, admin-pr-reviews-start-cli 31, admin-route-inventory.unit 4, api-export-surface.unit 10, changelog-format.unit 6, adr-index.unit 8; 0 failures. The same seven pr-reviews suites on clean origin/dev were 234 pass, 0 fail, so this branch adds 51 tests to them.
  • bun run typecheck and bun run lint clean; Biome clean on every changed file.
  • Tests drive the real bun run src/index.ts against the repository's Bun.serve GitHub stand-in and the real admin router over bun:sqlite; no mocks. They assert on what the administrator's token wrote (POST reviews, PUT merge, comments, closes), not on text alone. The stand-in can now omit mergeable_state, and change the state, head, status or openness after the administrator's first read.
  • Mutation pass on scratch copies of the tree (the worktree was never mutated), one change at a time, each confirmed applied: 36 mutants, all killed. They include: a missing mergeable_state read as clean (in fetchPullRequest and in mergeCheckOf); --force skipping the early check; the late look removed, in approve and in next; the late look reading a stale state (in both callers, and in the helper); the late look also running without --merge; the late look not labelled; the open check removed from the re-read; the property-name lookup restored; the failure reasons collapsed back to unknown; a failed read read as clean; a moved head not detected; the early check removed, or moved after the confirmation; unstable, unknown or a missing state treated as clean; clean refused; y not gated on the check, or --force skipping it; the look before the merge removed; the approve-only hint and the advice wrong in each direction. One mutant (--force skipping the early check) survived the first run because the late look still wrote nothing; the tests now assert the refusal came from the early check, and it is killed. The delta review's two findings are covered the same way: the late look moved above the confirmation in approve, and a late look taken when the card is shown in next, both fail the new tests; the advice text reverted for next, for a moved head and for approve, and a not-read state read as clean, are also killed.

Not verified

  • Nothing was run against real GitHub, and no pr-reviews write command was run. That a held review actually reads unstable is still the expectation recorded in ADR 0092, not an observation.
  • The confirmation prompt on a real terminal (the tests have no TTY; without --yes they see "Not approved." from the non-interactive path).
  • The tests for an unknown that never settles wait out the real re-ask interval (about 4 seconds each), and a few others wait one or two seconds, so the CLI suite is slower than before.

For the maintainer

  • A repository whose protection required an approving review would read blocked before the administrator's approval and clean after it. --merge and y used to approve and merge such a pull request; they now refuse it. The ruleset applied to published datasets requires none (required_approving_review_count: 0, ADR 0001) and the four legacy repositories use classic branch protection that requires none, so nothing is refused today for this reason.
  • Whether to accept unstable when both required checks pass (the other option in ADR 0092's open item) is still open and untouched here.

approve --merge approved a pull request and only then found GitHub
reports it unstable (the automated review holds it), so the approval was
left standing without the merge. The state is now read before anything is
written: unless GitHub says clean, nothing is approved or merged, the
state and why it blocks are named, and the way to approve without
merging is given. An unknown or unrecognised state is not clean. The look
after the approval stays for a state that changes in between.

Tested: real CLI against the GitHub stand-in, pr-review-approve and
admin-pr-reviews-cli suites.
y in next approves and squash-merges, but was offered for any state
except dirty and behind, so a pull request the automated review holds
(unstable) was approved and then refused the merge. y is now offered only
when GitHub reports clean, settled by asking again while it is unknown;
the card names the state, why it blocks and how to approve without
merging, and answering y anyway writes nothing. --force does not reach
the state. approveAllowed takes the state as a required argument and
treats a missing one as not clean.

Tested: real CLI against the GitHub stand-in, admin-pr-reviews-cli and
pr-review-next suites.
A mutation pass showed that reading a failed or moved re-read of the
mergeable state as clean, or ignoring it in approve, left every test
green. The stand-in can now fail or move the head for the administrator's
own reads only, and approve --merge and next each assert that nothing is
written and the reason is said.
Records that a merge asked for is checked before anything is approved,
settles the part of ADR 0092's open item it answers, and adds the
Unreleased changelog entry. The clean-only rule is unchanged.
@github-actions

github-actions Bot commented Oct 12, 2026 •

Copy link
Copy Markdown

📦 Package Preview

Your package is ready to test!

bun install -g nemar-cli@PR1720

Version: 0.10.17-PR1720.242f17b
Commit: 242f17b


🤖 Updated on each push.

A review mutation pass left two mutants alive: skipping the precheck
when --force is given, and reading an answer without mergeable_state as
clean. The stand-in can now omit the field, and approve --merge, next
and the settle reader each assert it reads as unknown with nothing
written; --merge --force on a held pull request refuses.
mergeRefusal looked the reason up in an object literal, so a state
word such as constructor yielded a function's source as the reason.
The table is a Map; the state is still refused as one this tool does
not merge on.
next folded a failed read and a moved head into the string unknown, so
the card said GitHub had not said when a 502 or a push had happened.
The answer is now one MergeCheck value (ok, why, a card phrase, and the
advice) made by mergeCheckOf and checkMergeable, the only place that
decides whether a merge may be asked for; approveAllowed takes it, so
the card carries the real reason.

settleMergeState takes the pull request as read (head and state in one
value) instead of a head and a loose state. A state that will not clear
by itself still advises approving without --merge; unknown and a failed
read say to try again in a moment. The approve-only hint is named once
instead of recomputed.

Tested: pr-review-approve, pr-review-next and admin-pr-reviews-cli.
settleMergeState did not look at the pull request's own state on a
re-read, so one closed or merged meanwhile was judged only by its
mergeable_state. A re-read that finds it closed or merged is now its own
result: the check before an approval refuses with nothing written and no
advice to retry, and the look before the merge says it was not merged
and the approval stands.

Tested: pr-review-approve, admin-pr-reviews-cli.
The state was read before the confirmation prompt and, in next, before
the card, and nothing looked again just before the approval is written,
which cannot be taken back. Both approve --merge and y now call one
helper, recheckBeforeApproval, that reads the state fresh (one request
when clean) and refuses with nothing written, saying it is the second
look. The early check stays for the user's sake; the look after the
approval stays for the window that remains.

Tested: a stand-in that changes the state, head, status or openness
after the first read, for approve and next; pr-review-approve and
admin-pr-reviews-cli.
The refusal sentence and the approve and next help said a held review
is reported as unstable and that unstable means a check is failing.
GitHub's unstable is a check or status that is not required and is not
passing or has not reported, and a held review producing it is
expected, not observed. The text now says that, and the help says
expected. The help also says the state is checked again just before the
approval is written.

Tested: pr-review-approve, pr-review-next, admin-pr-reviews-cli.
Records the read right before the approval, the reasons a failed read
now carries, the advice that differs by state, a pull request found no
longer open, and the unstable wording as expected rather than
observed. Corrects the amendment's list of remaining answers and its
approve-alone sentence, and states why a protection that requires an
approval is not a case today. Changelog entry matches.
With --force skipping the early check the late look still wrote
nothing, so the mutant survived: the outcome was the same, only later.
The --force and held tests now assert the refusal came from the early
check, before the card and without the late look's prefix.
No change in wording; the line had grown past the rest of the
paragraph.
ADR 0093 said the four legacy repositories' ruleset pins their BIDS
check; they have no ruleset, only classic branch protection with the
bids-validation and version-check contexts, so the bullet now says so
and agrees with the amendment below it.
Moving the late look above the confirmation kept every test green and
reopened the interactive window it exists to close. approve --merge
without --yes makes one read only when the confirmation is not given,
and next makes none beyond the card's when q is answered. Both fail
when the look is taken early (scratch-copy mutants).
next told the administrator to try y again, which was untrue in two
cases: when y is not offered typing it does not look again, and a card
refused for a moved head is pinned to the head it was shown for, so y
refuses every time. next now says s skips it and a fresh next run looks
again. A moved head gets its own advice (rerun, in the type, not only
the string) since waiting does not help it, and the late look's wording
says the head changed since it was read, not while it was being read.

Tested: pr-review-approve, pr-review-next, admin-pr-reviews-cli.
MergeSeen encoded a state that was not read as an absent optional;
it is now null, so it is stated at every call site. MergeResult's loose
mergeableState string is replaced by the MergeRefused every other
caller already uses. Compile-checked; no behaviour change.

Tested: pr-review-approve, pr-review-next, admin-pr-reviews-cli.
ADR 0093's main merge bullet listed only the read before and the
look after; it now includes the read just before the approval is
written and says plainly that GitHub has no approve-and-merge call, so
the window is narrowed to one request, not closed. The same sentence is
in the help and the changelog. The next advice (s skips, a fresh run
looks again) and the advice for a moved head are described too.
@neuromechanist neuromechanist changed the title fix: pr-reviews merge check comes before the approval fix: check mergeability before the approval Oct 12, 2026
CHANGELOG.md: kept dev's Unreleased entries (the weekly drift check, the
nightly issue, the workflow gh-call test, the modality filter) and this
branch's entry after them, in the one Fixed section. No other conflict.
@neuromechanist
neuromechanist merged commit d91c67a into dev Oct 12, 2026
14 checks passed
@neuromechanist
neuromechanist deleted the fix/pr-reviews-merge-precheck branch October 12, 2026 23:58
neuromechanist added a commit that referenced this pull request Oct 13, 2026
Keeps dev's #1711, #1719, #1720 and #1724 changelog entries and this branch's two, and dev's ADR index lines with this branch's ADR 0098 line.
@neuromechanist neuromechanist mentioned this pull request Oct 13, 2026
2 tasks done
@neuromechanist neuromechanist mentioned this pull request Oct 13, 2026
17 tasks done

This branch was previously deployed

1 inactive deployment
npm — 242f17b4 Deployed Oct 12, 2026 by neuromechanist via publish #3387
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant