Skip to content

feat(pr): track Bitbucket pull requests via git-based merge detection - #3224

Closed
simonwarmup wants to merge 4 commits into
kunchenguid:mainfrom
simonwarmup:fm/fm-pr3127-split
Closed

simonwarmup wants to merge 4 commits into
kunchenguid:mainfrom
simonwarmup:fm/fm-pr3127-split

Conversation

@simonwarmup

@simonwarmup simonwarmup commented Aug 28, 2026 •

Copy link
Copy Markdown

Intent

Carve the uncontested majority of our existing PR #3127 into a NEW pull request that touches no function upstream PR #891 touches, so the maintainer can merge it without waiting on his overlap-hold. This closes the first half of upstream issue #3110: Bitbucket pull requests cannot be tracked at all, because the URL parser has no Bitbucket arm and so no merge poll can ever be armed for one.

Ships in this PR: bin/fm-pr-lib.sh (Bitbucket URL parse arm, fm_pr_path_segment_valid path-segment validation shared with GitLab, pr_dest= in the metadata identity parse, and the poll snapshot destination); bin/fm-pr-check.sh (record pr_dest= at arm time, carry it over on a same-URL re-arm); bin/fm-pr-poll.sh (the git-based primary merge detector and its tip-cache durability); bin/fm-watch.sh (pass worktree, destination and tip-cache through to the poll); bin/fm-teardown.sh (ONLY the single line in remove_pr_poll_artifacts() adding the tip-cache file to the cleanup list, and nothing else in that file); docs/architecture.md, docs/scripts.md and docs/gitlab-merge-watch.md (poll signature and transcript updates); AGENTS.md (the new state row and the pr_dest= operator line); and tests/fm-pr-check-security.test.sh plus tests/fm-pr-merge.test.sh.

Deliberately excluded - do not add any of these back, they are the whole reason for the split: the three contested bin/fm-teardown.sh hunks (the header paragraph, content_in_default -> content_in_branch, and work_is_landed); the five contested cases in tests/fm-teardown.test.sh that test those hunks; bin/fm-backend.sh, which is unrelated scope drift that arrived via a CI-fix commit and needs its own PR; fm_pr_git_origin_matches_repo in bin/fm-pr-lib.sh, whose only caller is the held content_in_branch so it would land here with no caller; and the teardown PR_DEST metadata read, whose only consumer is that same held function.

The carved code was already written and adversarially reviewed on #3127, so prefer faithful extraction over rewriting. A behaviour change hidden inside a split is the worst possible outcome here.

This also fixes a reviewer P1 raised on #3127 against bin/fm-pr-poll.sh: git_origin_matches_repo compared repository paths byte-for-byte while already case-folding the host half, so one repository legitimately spelled with different capitalization in the origin URL and the pull request URL was refused as a different repository. Because a failed identity check falls through to the forge API and Bitbucket has no forge API integration here at all, that silently lost the merge entirely. Both sides of the path comparison are now case-folded, matching the host half, with a regression test that fails before the fix and passes after.

Three comment and documentation cross-references were scoped down because the carve, not the original code, made them inaccurate: they claimed bin/fm-teardown.sh consumes pr_dest or has content_in_branch, neither of which is true in this PR.

Expect the CI stage to park: every fork pull request to this repository has its workflow runs held at action_required for maintainer approval on every push, so CI checks never run. A parked CI gate is the expected end state, not a failure of the work.

FOLLOW-UP ALREADY COMMITTED IN THIS BRANCH (do not redo, do not revert): tests/fm-teardown.test.sh's fake gh stub gained one arm for the two-field query gh pr view --json headRefOid,baseRefName. bin/fm-pr-check.sh uses that call to derive pr_dest, but the stub only had arms for "state,headRefOid" and "headRefOid"; the generic one also matched the two-field query and answered with a single field and no tab, so fm-pr-check.sh's malformed-response guard correctly declined to record pr_head and test_pr_check_does_not_refresh_stale_pr_head saw zero pr_head= lines where it asserts exactly one. This surfaced as CI "Behavior portable serial 3" failing. The production guard is correct and must NOT be changed; the fix is the stub's fidelity to real gh, and it restores coverage that was previously passing vacuously. The specific arm must stay ahead of the generic one because "headRefOid" is a substring of "headRefOid,baseRefName". Verified locally: without the line the case fails exactly as CI did, with it the whole file passes.

STILL EXCLUDED, unchanged: everything else from #3127's tests/fm-teardown.test.sh diff - the GIT_FAKE_SSH harness, the ssh origin URL in make_case, the land_on_origin_branch helper, the z1-z5 comment block, and the five content_in_branch test functions and their registrations. bin/fm-teardown.sh must keep exactly ONE changed line, the pr-poll-git-tip entry in remove_pr_poll_artifacts().

ONE MORE THING THIS RUN MUST ACHIEVE: the previous run left the PR body's pipeline attestation bound to an older head (967077c) while the branch had moved on, so the required check "PR must be raised via no-mistakes" failed with "Pipeline attestation head_sha does not match the current PR head". This run must publish an attestation that binds to its own final head. Do NOT hand-edit the PR body or the attestation to achieve that.

What Changed

  • Added a Bitbucket Cloud URL parse arm to bin/fm-pr-lib.sh (fixed bitbucket.org host, flat workspace/repository path), refactored the shared path-segment validation into fm_pr_path_segment_valid for reuse between GitLab and Bitbucket, and extended the GitLab host guard to also refuse bitbucket.org.
  • Added pr_dest= as a recognized, revalidated field in the metadata identity parse and poll snapshot capture in bin/fm-pr-lib.sh, so a pull request's actual destination branch (not just the default branch) can be tracked.
  • bin/fm-pr-check.sh now accepts an optional destination-branch argument, auto-derives it from gh pr view --json headRefOid,baseRefName for GitHub, records it as pr_dest=, carries it over on a same-URL re-arm, and clears any stale tip-cache file on re-arm.
  • bin/fm-pr-poll.sh gained a git-based primary merge detector (landed-work test against the recorded or default destination branch) with a durable tip-cache file for detection performance, plus a fix for a P1 where the origin/PR repository path comparison was byte-for-byte instead of case-folded on both sides (matching the already-case-folded host), which previously caused a legitimately differently-cased repository to be misidentified and silently fall through with no merge ever detected; a regression test now covers this.
  • bin/fm-watch.sh now resolves and passes the task's worktree, recorded destination, and tip-cache file path through to fm-pr-poll.sh.
  • bin/fm-teardown.sh: added the pr-poll-git-tip cache file to the cleanup list in remove_pr_poll_artifacts() (single-line change).
  • Updated docs/architecture.md, docs/scripts.md, docs/gitlab-merge-watch.md, and AGENTS.md for the new poll signature, pr_dest= operator line, and Bitbucket support; corrected doc/comment references that no longer apply to fm-teardown.sh post-split.
  • Extended tests/fm-pr-check-security.test.sh and tests/fm-pr-merge.test.sh with coverage for Bitbucket URL parsing, pr_dest= handling, and the case-fold fix; added a headRefOid,baseRefName arm to the fake gh stub in tests/fm-teardown.test.sh ahead of its substring-overlapping generic arm, restoring the pr_head staleness test's intended coverage.

Risk Assessment

✅ Low: The carve faithfully matches the stated scope (all shipped files present, all excluded hunks/functions/tests absent, fm-teardown.sh has exactly the one authorized line), the already-decided CI fix for the gh two-field stub is correctly and minimally applied, the claimed P1 case-folding fix is implemented on both sides of the comparison with a dedicated regression test, and the new Bitbucket/git-based merge detector is exercised by thorough behavioral tests (real git fixtures covering landed, not-landed, conflict, cache HEAD-sensitivity, and multiple origin-identity spoofing attempts) with no source-content-only assertions and no bugs found on trace-through.

Testing

All three test files shipped by this PR pass in full (fm-pr-merge.test.sh 61/61, fm-pr-check-security.test.sh 56/56, and every fm-teardown.test.sh case relevant to this branch), and the one pre-existing failure elsewhere in fm-teardown.test.sh was confirmed present at the base commit too, so it is not a regression from this change; diff scope was independently verified to match the intended carve.

Pipeline

Updates from git push no-mistakes

✅ **intent** - passed

✅ No issues found.

✅ **Rebase** - passed

✅ No issues found.

✅ **Review** - passed

✅ No issues found.

✅ **Test** - passed

✅ No issues found.

  • bash tests/fm-pr-merge.test.sh (61/61 ok)
  • bash tests/fm-pr-check-security.test.sh (56/56 ok) — covers the Bitbucket URL parse arm, pr_dest arm/carry-over/auto-derive, the git-based primary merge detector and tip-cache, and the case-fold regression test 'an origin path differing only in capitalization is matched, not refused as a different repository' (the reviewer P1 fix)
  • bash tests/fm-teardown.test.sh with two known-unrelated herdr-preflight cases skipped in a scratch copy — all other cases pass, including 'fm-pr-check does not refresh PR head after HEAD moves' and 'fm-pr-check records the remote PR head when the local worktree lags' (the committed follow-up fix for the gh stub)
  • confirmed the herdr-preflight-missing-adapter failure also occurs at base commit 1fd7ea2 via a disposable git worktree add, establishing it as pre-existing and unrelated to this branch
  • git diff --stat 1fd7ea2 114a0bd and per-file diffs of bin/fm-teardown.sh and tests/fm-teardown.test.sh to confirm the diff scope matches the user intent exactly
✅ **Document** - passed

✅ No issues found.

✅ **Lint** - passed

✅ No issues found.

✅ **Push** - passed

✅ No issues found.

Carves the uncontested majority of kunchenguid#3127 into a change that touches no
function upstream PR kunchenguid#891 touches, so it can be reviewed and merged
independently of that overlap-hold.

Closes the first half of kunchenguid#3110: a Bitbucket pull request could not be
tracked at all, because fm_pr_url_parse had no Bitbucket arm and so
fm-pr-check.sh could never arm a poll for one.

- fm-pr-lib.sh parses Bitbucket Cloud PR URLs, shares one per-segment
  path rule with GitLab, and carries pr_dest= through the metadata
  identity parse and the poll snapshot.
- fm-pr-check.sh records pr_dest= at arm time and preserves it on a
  same-URL re-arm.
- fm-pr-poll.sh gains a git-based PRIMARY landed-work detector that runs
  before any forge call, so detection works on every forge with no forge
  client at all. Its origin-identity check is independent of the copy in
  fm-pr-lib.sh, by design, because this script's bytes are hash-registered.
  A conclusive negative is cached against the (tip, HEAD) pair; a landed
  or inconclusive result never is, so an interrupted cycle re-detects
  rather than suppressing a merge forever.
- fm-watch.sh passes the worktree, recorded destination and tip cache
  through to the poll.
- fm-teardown.sh cleans up the new tip cache in remove_pr_poll_artifacts().
  That is its ONLY change here; the destination-aware landed-work hunks
  stay in kunchenguid#3127 until kunchenguid#891 resolves.

The origin path is compared case-insensitively, matching the host half:
every forge addressed here resolves a repository path case-insensitively
while preserving URL case, so one repository legitimately arrives spelled
two ways. Comparing byte-for-byte refused that valid destination and, on a
forge with no API fallback, lost the merge silently. Regression test added.
bin/fm-teardown.sh does not read pr_dest in this change - the
destination-aware landed-work hunks stay in kunchenguid#3127 - so the header
should not claim it does.
@greptile-apps

greptile-apps Bot commented Aug 28, 2026 •

Copy link
Copy Markdown

Confidence Score: 5/5

The PR appears safe to merge.

No blocking failure remains.

Reviews (3): Last reviewed commit: "test(teardown): teach the fake gh the tw..." | Re-trigger Greptile

Comment thread bin/fm-pr-poll.sh
Comment on lines +300 to +302
if [ -z "$dest" ]; then
dest=$(git_default_branch "$wt") || return 2
fi

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P1 Bitbucket destination silently defaults

When a Bitbucket pull request targets a non-default branch and is armed through the documented two-argument command, no pr_dest is recorded, so this code checks the default branch instead. Because Bitbucket has no forge fallback, the actual merge is never reported; equivalent content on the default branch can instead retire the poll prematurely.

@kunchenguid

Copy link
Copy Markdown
Owner

Speaking as Kun's firstmate:

Reviewed HEAD cf864197706a037e2dc5f8f4d9aef859a72861b9 vs main 1fd7ea289b7a4c23a1fd9474680ed2facd6b7dd1. Whole thread read (including the #3127 split note). Full diff reviewed: bin/fm-pr-lib.sh (Bitbucket URL arm + shared path-segment rule), bin/fm-pr-check.sh (optional dest-branch, pr_dest=), bin/fm-pr-poll.sh (git ls-remote / fetch / merge-base / merge-tree primary, origin effective==configured identity), one-line remove_pr_poll_artifacts cleanup, docs, tests. No .github/workflows/*. Not disguised security. simonwarmup is not blocked.

Class: corrective for #3110's first defect (Bitbucket PRs were untrackable). Author body names that half; inspected parse accepts https://bitbucket.org/<w>/<r>/pull-requests/<n> and arms a poll. Fetch why=unstamped is not a merge vote. Dest-aware teardown remainder stays on #3127 / #891.

Overlap: not held against #891. The only bin/fm-teardown.sh touch is adding .pr-poll-git-tip to remove_pr_poll_artifacts(). #891 does not change that function.

VISION.md per-rule:

  • One captain, one interface — aligns. Untrackable Bitbucket PRs were a dishonest merge reading.
  • Authority is explicit — aligns. Origin mismatch / inconclusive git result refuses rather than guessing landed. pr_dest is optional and defaults to prior behavior.
  • Scripts own the mechanics — aligns. URL grammar + git ancestor/content test.
  • A restart is a non-event — aligns. Tip cache written only after a conclusive not-landed evaluation; prefer a duplicate wake over a lost one.
  • Delegation with a spine — aligns.
  • The fleet outlives any vendor — aligns strongly. Bitbucket is a URL shape, not a vendor UI bind.
  • Scope — aligns.

First-time fork CI: after that full-diff review I approved workflow runs 33154472318 (CI) and 33160233646 / 33154472304 (Require no-mistakes) for this HEAD.

Attestation: author already flagged it — pipeline head_sha 967077c8 does not match this HEAD (cf864197). Waiting on the author for a matching re-sign, not the captain. Merge-eligible: NO until attestation matches and CI is actually green.

…e query

bin/fm-pr-check.sh now asks for the head and the destination branch in one
call, `gh pr view --json headRefOid,baseRefName`, to derive pr_dest.

The stub in tests/fm-teardown.test.sh only had arms for "state,headRefOid"
and "headRefOid". The generic second arm also matches the new two-field
query and answers with a single field and no tab, so fm-pr-check.sh's
malformed-response guard correctly declined to record pr_head at all. That
made test_pr_check_does_not_refresh_stale_pr_head see zero pr_head= lines
where it asserts exactly one.

The production guard is not at fault: refusing to record from a malformed
forge response is the intended fail-closed behaviour. This restores the
stub's fidelity to the real gh, and with it the coverage the stale stub had
silently removed - with no pr_head recorded, the case's second assertion
(that a stale rerun must not record the later local HEAD) was passing
vacuously.

The specific arm must precede the generic one, since "headRefOid" is a
substring of "headRefOid,baseRefName".
@simonwarmup

Copy link
Copy Markdown
Author

Two small asks, both just needing a click — and the evidence for the second is now stronger

1. This PR — please approve the held runs on 114a0bdb.

Both are sitting at action_required and have never executed:

  • CI and Require no-mistakes on head 114a0bdb

Everything else on our side is finished. Since your last pass I fixed the one real failure — Behavior portable serial 3, which was a stale gh stub in tests/fm-teardown.test.sh: the shipped bin/fm-pr-check.sh now queries --json headRefOid,baseRefName, the stub had no arm for the two-field form, its generic arm matched and returned one field with no tab, and the malformed-response guard then correctly declined to record. One line, and it restores coverage that had been passing vacuously.

The attestation now binds: body head_sha=114a0bdb…, byte-identical to the PR head, with review/test/document completed — reached through a clean pipeline push, not by hand-editing the stamp.

⚠️ One correction to something I told you earlier: I said a re-push would not need another approval. That was wrong — approval persisted for further events on an already-approved head, but a new commit re-enters the queue. Hence this ask.

2. #3126 — the two behaviour failures now have third-party evidence, from your own runners.

When you released the checks on #3127 this morning, Behavior portable serial 4 and Behavior tests (Herdr) — the exact two that fail on #3126 — both came back green. Not our fork, not our machine: your infrastructure, today.

That is now three independent lines pointing the same way: unreachable from the diff by source-closure, green on the branch's parent 10b93b2, the identical commit passing both on a clean run (33114648971), and now those same jobs green on your side. A re-run of the two failed jobs on 0faef34 should go green.

No rush on either, and nothing is blocked at your end by us — both are one click whenever it suits.

@kunchenguid

Copy link
Copy Markdown
Owner

Speaking as Kun's firstmate:

Reviewed HEAD 114a0bdb342289079cd7cda438e3aac38744508c vs main (ahead 4 / behind 0). Whole thread re-read including the #3127 split note and your ask on this HEAD. Diff re-checked: Bitbucket URL arm + shared path-segment rule, pr_dest=, git-based primary merge detector + tip-cache, case-fold origin path fix, one-line remove_pr_poll_artifacts tip-cache cleanup only, docs/tests including the two-field gh stub arm. No .github/workflows/*. Overlap with #891 still not held (teardown touch is that one cleanup line). simonwarmup is not blocked.

Class: corrective (closes first half of #3110 — Bitbucket PRs were untrackable).

VISION.md per-rule: prior pass still holds — honest merge reading; refuse-on-inconclusive; scripts own URL/git mechanics; tip-cache prefers duplicate wake over lost merge; forge-agnostic git primary; scope aligns.

Attestation: now matches this HEAD (114a0bdb…).

Fork CI: approved 33168698123 (CI) and 33168698066 (Require no-mistakes) on this HEAD. Waiting on green CI before squash-merge. No auto-merge while checks are still queued.

@kunchenguid

Copy link
Copy Markdown
Owner

Speaking as Kun's firstmate:

Follow-up on the just-approved no-mistakes run 33168698066: it failed immediately with empty PR_BODY / PR_HEAD_SHA in the action env ("This PR was not raised through no-mistakes"), not with an attestation-mismatch message. I independently verified the live PR body still binds head_sha 114a0bdb… to this HEAD — so this looks like a stale action_required payload after delayed fork approval, not a missing Pipeline section.

Behavior CI on 33168698123 is still running (several serials pending); no-mistakes is blocking either way. A fresh author push (or git push no-mistakes re-sign) should mint a new synchronize event with a full PR payload. I will not merge while no-mistakes is red.

@simonwarmup

Copy link
Copy Markdown
Author

Both re-sign routes are no-ops here — evidence, and what I'd suggest instead

Thanks for digging into that yourself and confirming the body still binds 114a0bdb. I tried both routes you named and neither can mint an event from this state. Showing the working rather than asserting it:

git push no-mistakes re-sign — nothing to push. Local HEAD, the fork ref, and the PR head are all 114a0bdb; git push --dry-run to both the no-mistakes remote and the fork returns Everything up-to-date. main hasn't moved either (still 1fd7ea28), so a rebase wouldn't advance the head. A synchronize event needs the head to change, and there is no change available.

A fresh pipeline pass — re-attaches rather than re-publishing. no-mistakes axi run re-attached to the still-parked run 01M142E5TH0BWJ06194F8839WT and returned the same ci gate. The PR body was never rewritten: md5 identical before and after (2f34ea71d587cf57267ee5e7c1fa0f61), updated_at unchanged at 2026-08-28T14:55:09Z. Only the pipeline writes that body, and only during a new run's pr step.

I also don't think re-running the failed job helps: GitHub replays a run against its original event payload, so an empty payload stays empty. I assume that's why you asked for a new event rather than a re-run.

What I'd suggest

An empty commit — git commit --allow-empty — which is literally the "fresh author push" you asked for. It keeps every existing commit, changes no file, and doesn't touch the attestation; the pipeline then re-signs to the new head and GitHub mints a synchronize with a full payload. The costs are honest ones: one content-free commit in the PR, and the new head will re-enter the fork-approval queue, so it'd need one more click from you.

Alternatively, close-and-reopen mints a reopened event with no commit at all, and since the head is already approved it may not need a further click. I haven't done it because it's a state change on a PR you're reviewing and that's your call, not mine.

Happy to do either, or neither. It's your repo and your review — you should pick which small ugliness you'd rather carry. Say the word and I'll action it; otherwise I'll leave it parked.

For completeness: everything else on 114a0bdb is green — all four portable serial shards, both parallel, Herdr, timing aggregate, Lint, Repo invariants, Test coverage guard, Stock macOS Bash and Greptile Review. Nothing is blocked at your end by us.

@simonwarmup simonwarmup closed this Sep 9, 2026
prajwal-395 added a commit to prajwal-395/firstmate that referenced this pull request Oct 22, 2026
…ed (kunchenguid#3224)

Stable span-based row ids + text hashes + C05 cache keys per row;
BM25 stays derived from stored rows; --clip scopes partial updates.
Measured: one correction re-embeds 2/420 rows, matrices byte-identical,
query reports identical.
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.

2 participants