Repository navigation
feat(bin): merge native GitHub stacked pull requests through the async merge API - #94
Merged
Merged
Conversation
added 6 commits
October 1, 2026 03:06
A pull request that GitHub's own record shows inside a stack merges one layer at a time, lowest unmerged first, through GitHub's asynchronous merge API instead of gh pr merge, because GitHub accepts stacked merges only there and rewrites stacked heads on its own server. Detection reads the pull request's REST resource before anything is recorded: a stack object with an integer number, an integer position and a non-empty base.ref selects the stacked path, a missing or null stack runs the synchronous path unchanged, and an unreadable record or unrecognisable shape refuses instead of guessing. The stacked request is one merge commit bound to the verified head with merge_method, merge_action and sha; every existing live gate still runs on the stacked path, the stack's own record must show every lower layer merged and this layer retargeted onto the stack base, a merge-queue base or unreadable branch rules refuse, forge flags from --auto to --delete-branch refuse even with --attended-override, and the away-posture record refuses with exit 2 at both authority reads. For a no-mistakes task the pipeline must have validated the live head with its ci step running before the request is bound, while a direct-PR task skips that rule; a 202 pending answer persists the merge authority under the task's lock and polls the named result, a failed result quotes GitHub's own report and retires the authority, and a landing is reported only after a live read-back agrees, while every unreadable or still-pending result keeps the authority and the armed merge poll to report the landing later. tests/fm-pr-merge.test.sh extends the gh stub with the REST pull read, the stack read, the submit and the poll, and adds fifteen stacked cases covering detection, bottom-up order, refusal paths, the request outcome, and regression pins that keep the red, pending and unreported-required check gates on the stacked path; docs/architecture.md names the stacked path beside the synchronous one. Verification: tests/fm-pr-merge.test.sh passes 124 of 124 cases including the fifteen new stacked cases; bin/fm-lint.sh clean; shellcheck -x clean on bin/fm-pr-merge.sh; bin/fm-jev-lint.sh check clean.
…ge.sh, `github_stack_membership_class` required a non-empty `.base.ref` before it checked for a stack. A PR whose record had no stack and no `base` object was therefore classed "undeterminable", and the merge was refused. Real GitHub always returns `base.ref`, but the gh stubs in other test suites leave it out, so fm-pr-merge refused the merge in those suites. Serial shard 5 failed in tests/fm-pr-check-security.test.sh (test merge-outcome-committed) with "stack membership could not be determined ... the stack field is not a shape this script recognises". The truncated log for parallel shard 2 doesn't name its failing test; it is most likely fm-captain-hold-lifecycle, which also calls fm-pr-merge through a stub, but I haven't confirmed that. **Invariant:** a pull request whose `stack` is absent or null is classed standalone, whatever its other fields contain. The membership class is the only place where this decision is made. **Fix:** I moved the `base`/`base.ref` checks below the standalone test, so only PRs that have a stack object must have a base. Stacked shapes that are missing a base still refuse as undeterminable; tests/fm-pr-merge.test.sh covers this case. I made no test changes. **Verification:** - tests/fm-pr-check-security.test.sh passes locally (rc=0); it failed with the error above before the fix. - tests/fm-pr-merge.test.sh passes locally (rc=0). - fm-captain-hold-lifecycle, fm-branch-supervision and fm-afk-contract were still running when this result was required, so they are unverified
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Intent
bin/fm-pr-merge.sh cannot merge a pull request that belongs to a native GitHub stack, because GitHub accepts stacked merges only through its asynchronous merge API. The helper gains a stacked path, chosen automatically from GitHub's record of the stack, that merges one layer at a time bottom-up with a merge commit through the asynchronous API, binds the verified head into the request, keeps every existing guard, and reports a landing only after a live read-back proves the merge.
What Changed
bin/fm-pr-merge.shnow reads the pull request's RESTstackobject to tell stacked, standalone, and undeterminable PRs apart. Standalone PRs keep the existing synchronousgh pr mergepath. If the stack record can't be read or has an unrecognised shape, the helper refuses before it records anything.--squash,--rebase, or any forge flag is passed, even with--attended-overridedocs/architecture.mdsummarises the stacked path, andtests/fm-pr-merge.test.shadds coverage for detection, ordering, refusals, submit/poll outcomes, and the head check on read-back.🤖 Generated with Claude Code
Risk Assessment
Testing
I drove the fix live on a fresh two-layer native stack in the authorised scratch repo andrewesweet/stacked-pr-lab. For the bottom layer, a descendant commit was pushed after verification and before the async submit. GitHub merged only the bound head 906d412. The script now exits 1 with a disagreement naming 906d412 and the newer commit 3451da2, prints no landing line, keeps the merge authority and publishes no outcome; in round 2 the same race was reported as a landing. The restacked top layer then merged through the async API with a two-parent merge commit and exactly one verified landing (exit 0). This shows the head comparison does not block a valid merge. All 16 stacked fixture tests pass. The new regression test fails against the previous script and passes against the fix. These fixture results are recorded below as untested scenarios because they were not run against the live product. The scratch repo was kept; the local lab directory was removed and the worktree is clean. This is a CLI change, so the evidence is transcripts rather than screenshots.
Evidence: Live descendant race now refused (PR #9)
Source: Live descendant race now refused (PR #9)
Evidence: Live next layer lands once (PR #10)
Source: Live next layer lands once (PR #10)
Evidence: Round 3 live summary
Source: Round 3 live summary
Evidence: Stacked fixture tests (round 3)
Source: Stacked fixture tests (round 3)
Evidence: Regression test fails before the fix
Source: Regression test fails before the fix
Evidence: Regression test passes after the fix
Source: Regression test passes after the fix
Evidence: Live script output for the race
Pipeline
Updates from git push no-mistakes
✅ **intent** - passed
✅ No issues found.
✅ **Rebase** - passed
✅ No issues found.
bin/fm-pr-merge.sh:1607- Spec: the validated-head fallback is not unquoted.validated=$(fm_nm_field "$out" head_sha)keeps the TOON quotes, sohead_sha: "<sha>"is read as"<sha>". When the status document has nobranch_sync.pipeline.current_head(an older CLI, or a run with no branch sync),"<sha>"never equals FM_PR_MERGE_HEAD, andgit rev-parseon the quoted string fails. The stacked merge then always refuses with 'has not validated the live head', even though the pipeline validated exactly that head. The sibling reader at bin/fm-dod-lib.sh:761 wraps the same call in fm_nm_strip_quotes; do the same here. The ci step state parsed by github_nm_ci_step_state (bin/fm-pr-merge.sh:1608) is also compared raw torunning, so a quoted status cell would fail the same way.bin/fm-pr-merge.sh:1591- Spec: github_verify_pipeline_validated_head adds a new gate that the intent does not ask for. The intent requires the helper to bind the verified head and keep every existing guard. This gate goes further: for a no-mistakes task it requires a readableno-mistakes axi statuswhose validated head matches the live head and whose ci step isrunningand active. The synchronous path has no such rule. A stacked layer of a no-mistakes task therefore cannot merge once its pipeline has finished, or when theno-mistakesCLI or worktree is missing, while the same PR standing alone would merge. It also brings in new helpers (github_nm_ci_step_state and github_nm_ci_step_active, local copies of the fm-crew-state row parsing) and a new library dependency (the fm-nm-run-lib.sh source line). Suggested remedy: remove this component, or have the author confirm it is wanted as part of the stacked contract.bin/fm-pr-merge.sh:1423- Standards: this comment is false and the assignment it justifies is redundant. github_detect_stack_membership says 'the stacked path skips the mergeable read that otherwise sets this for it' and sets FM_PR_GITHUB_BASE. In fact the stacked path does run github_verify_mergeable (bin/fm-pr-merge.sh:1867), which overwrites FM_PR_GITHUB_BASE with the live base before github_refuse_queue_for_stack reads it. The header's own claim that every existing live gate still runs on the stacked path contradicts this comment. Remove the comment and the assignment, or correct the comment.bin/fm-pr-merge.sh:1501- Spec: github_verify_stack_order judges which layers are 'below' this PR by their order in thepull_requestsarray, not by each layer's position. FM_PR_STACK_POSITION is read but used only in an error message. If GitHub lists layers in any other order, a lower unmerged layer that appears after this PR is skipped. Only the base == stack-base check would then stop an out-of-order merge. Sort the entries by.positionin the jq expression at line 1515 (or compare against FM_PR_STACK_POSITION) so the 'every lower layer merged' rule does not depend on array order. Relatedly, the base comparison at line 1525 uses FM_PR_BASE_REF from the earlier detection read, not the live FM_PR_GITHUB_BASE just verified by the mergeable read.🔧 Fix applied.
1 warning still open:
bin/fm-pr-merge.sh:1591- Spec: github_verify_pipeline_validated_head adds a new gate that the intent does not ask for. The intent requires the helper to bind the verified head and keep every existing guard. This gate goes further: for a no-mistakes task it requires a readableno-mistakes axi statuswhose validated head matches the live head and whose ci step isrunningand active. The synchronous path has no such rule. A stacked layer of a no-mistakes task therefore cannot merge once its pipeline has finished, or when theno-mistakesCLI or worktree is missing, while the same PR standing alone would merge. It also brings in new helpers (github_nm_ci_step_state and github_nm_ci_step_active, local copies of the fm-crew-state row parsing) and a new library dependency (the fm-nm-run-lib.sh source line). Suggested remedy: remove this component, or have the author confirm it is wanted as part of the stacked contract.🔧 **Test** - 1 issue found → auto-fixed (2) ✅
bash <temp runner>: tests/fm-pr-merge.test.sh limited to all 15test_stacked_*cases plus the cases listed before them (69 ok, 0 failures)gh api repos/andrewesweet/firstmate/pulls/88then the script's owngithub_stack_membership_classon the real response (read-only)gh api repos/andrewesweet/firstmate/stacks/1(read-only check that the stacks endpoint exists)🔧 Fix applied.
2 issues (1 warning, 1 info) still open:
bin/fm-pr-merge.sh:1550- Live finding in scratch repo PR feat(bin): link routed-task traces to the routing agent and emit handoff spans #4: a descendant commit (725b384) was pushed to the layer after the script verified head f0cd64e and before the merge-async submit. GitHub answered 202 and merged exactly the bound head f0cd64e. The script then reported 'verified: ... is merged' (rc=0). The PR now shows head 725b384 and merged=true, but 725b384 is not on main. The binding worked as specified: nothing unverified landed. The difference is that the synchronous path's --match-head-commit refuses this race outright, while the stacked path reports a landing and closes the PR with a newer pushed commit left off main. Please decide whether the stacked path should compare the PR's head after merge with the bound head and report the disagreement.gh pr merge --match-head-commit --squashgh api -X POST repos/andrewesweet/stacked-pr-lab/stackswith{"pull_requests":[4,5,6]}(native stack #7) and pre-merge mergeable/mergeStateStatus readsbin/fm-pr-merge.sh s6 https://github.com/andrewesweet/stacked-pr-lab/pull/6ands5 .../pull/5before the lower layers merged (live refusal)bin/fm-pr-merge.sh s4 .../pull/4 -- --squash | -- --rebase | -- --method squash | --attended-override -- --admin | --auto | --delete-branch(live refusals)bin/fm-pr-merge.sh s4 .../pull/4with mode=no-mistakes and no pipeline run (live validated-head refusal)bin/fm-pr-merge.sh s4 .../pull/4with a real descendant push to layer 1 just before the merge-async PUT (bound head merged, racing commit not landed)bin/fm-pr-merge.sh s5 .../pull/5with a real non-descendant force-push to layer 2 just before the PUT (GitHub 400, quoted, nothing merged)bin/fm-pr-merge.sh s5 .../pull/5ands6 .../pull/6bottom-up live merges via merge-async, then poll, then read-backbin/fm-pr-merge.sh solo .../pull/8standalone control on the synchronous pathtest_stacked_* functions fromtests/fm-pr-merge.test.sh(15 fixture-only scenarios, run in isolation)🔧 Fix applied.
✅ Re-checked - no issues remain.
Live, scratch repo andrewesweet/stacked-pr-lab: built native stack #11 (PR #9 at the bottom, PR #10 on top) viaPOST /repos/andrewesweet/stacked-pr-lab/stacksLive:bin/fm-pr-merge.sh r1 https://github.com/andrewesweet/stacked-pr-lab/pull/9with a gh wrapper that pushes a descendant commit to the layer branch just before the merge-async PUTLive:bin/fm-pr-merge.sh r2 https://github.com/andrewesweet/stacked-pr-lab/pull/10after GitHub restacked the layer onto main (no race)Checked live state afterwards viagh api repos/.../pulls/N,commits/<merge sha>,compare/main...<newer>,stacks/11, plus the lab FM_HOME state filesFixture: the 16test_stacked_*cases from tests/fm-pr-merge.test.sh, run on their ownRegression check:test_stacked_merged_with_a_newer_pushed_head_reports_disagreementrun against the script from 8ef21411 (fails) and from 40c22470 (passes)✅ **Document** - passed
✅ No issues found.
✅ **Lint** - passed
✅ No issues found.
✅ **Push** - passed
✅ No issues found.