Skip to content

Commit b2bf95f

Browse files
authored
fix: fetch current PR head for review diffs (kunchenguid#722)
* fix: always fetch PR head for review diffs Prefer a freshly fetched refs/pull/<n>/head over a reachable recorded pr_head= so reviewers never hold a merge over a "missing" fix that already landed on the remote PR. Recorded SHA is offline fallback only; local branch is last resort with a warning. Store the tip under refs/fm-review/ so a later base-branch fetch cannot clobber the compare tip via FETCH_HEAD. * no-mistakes(test): Isolate session-start nudge tests from gate state * no-mistakes(document): Correct review-diff documentation
1 parent 3729081 commit b2bf95f

5 files changed

Lines changed: 66 additions & 17 deletions

File tree

‎bin/fm-review-diff.sh‎

Lines changed: 28 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,12 @@
44
# Pooled project clones do not keep their local default branch current, so this
55
# helper compares remote-backed projects against origin/<default> after fetching
66
# the default branch, and local-only projects against the local default branch.
7-
# When state/<id>.meta records pr= for an open PR, the compare side is the PR
8-
# head (recorded pr_head= when reachable, else refs/pull/<n>/head) so review
9-
# stays current after no-mistakes fix rounds push to the PR; if the PR head
10-
# cannot be resolved, the script falls back to the local branch with a warning.
7+
# When state/<id>.meta records pr= (URL or number) for an open PR, the compare
8+
# side is ALWAYS a freshly fetched refs/pull/<n>/head by default so review stays
9+
# current after no-mistakes fix rounds push to the PR. A recorded pr_head= is
10+
# only a fallback when fetch fails (stale recorded SHAs must never win over a
11+
# reachable remote PR head). If neither PR head can be resolved, fall back to
12+
# the local branch with a warning. Without pr=, compare the local branch.
1113
# Usage: fm-review-diff.sh <task-id> [--stat]
1214
# --stat prints only the stat summary; default prints stat summary plus full diff.
1315
set -eu
@@ -89,19 +91,35 @@ pr_number_from_target() {
8991
printf '%s' "$n"
9092
}
9193

94+
fetch_pull_head() {
95+
local n=$1 resolved
96+
git -C "$WT" remote get-url origin >/dev/null 2>&1 || return 1
97+
# Fetch into a private ref so a later base-branch fetch cannot clobber the
98+
# compare tip via FETCH_HEAD, and so we never review a stale local object.
99+
git -C "$WT" fetch --quiet origin \
100+
"+refs/pull/$n/head:refs/fm-review/pull/$n/head" >/dev/null 2>&1 || return 1
101+
resolved=$(git -C "$WT" rev-parse --verify "refs/fm-review/pull/$n/head^{commit}" 2>/dev/null) || return 1
102+
[ -n "$resolved" ] || return 1
103+
printf '%s' "$resolved"
104+
}
105+
92106
resolve_pr_head() {
93107
local pr_url=$1 recorded_head=$2 n resolved
108+
n=$(pr_number_from_target "$pr_url") || true
109+
if [ -n "$n" ]; then
110+
if resolved=$(fetch_pull_head "$n"); then
111+
printf '%s' "$resolved"
112+
return 0
113+
fi
114+
fi
115+
# Offline / unreachable remote: recorded pr_head is better than the local
116+
# branch, but never preferred over a successful pull-head fetch above.
94117
if [ -n "$recorded_head" ] \
95118
&& git -C "$WT" cat-file -e "$recorded_head^{commit}" 2>/dev/null; then
96119
printf '%s' "$recorded_head"
97120
return 0
98121
fi
99-
n=$(pr_number_from_target "$pr_url") || return 1
100-
git -C "$WT" remote get-url origin >/dev/null 2>&1 || return 1
101-
git -C "$WT" fetch --quiet origin "refs/pull/$n/head" >/dev/null 2>&1 || return 1
102-
resolved=$(git -C "$WT" rev-parse --verify 'FETCH_HEAD^{commit}' 2>/dev/null) || return 1
103-
[ -n "$resolved" ] || return 1
104-
printf '%s' "$resolved"
122+
return 1
105123
}
106124

107125
PR_URL=$(grep '^pr=' "$META" | tail -1 | cut -d= -f2- || true)

‎docs/architecture.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -171,7 +171,7 @@ The `data/secondmates.md` line contract is owned by the [`secondmate-provisionin
171171

172172
`data/projects.md` records each project's delivery mode and optional `+yolo` autonomy flag.
173173
`no-mistakes` projects run the full validation pipeline, `direct-PR` projects open PRs without that pipeline, and `local-only` projects stay local until firstmate performs an approved fast-forward merge.
174-
When a selected delivery path calls for a diff, `bin/fm-review-diff.sh` refreshes the authoritative base and, when task meta records `pr=`, compares against the reachable recorded `pr_head=` or a freshly fetched `refs/pull/<n>/head` before falling back to the local branch with a warning.
174+
When a selected delivery path calls for a diff, `bin/fm-review-diff.sh` refreshes the authoritative base and, when task meta records `pr=`, always fetches and compares against `refs/pull/<n>/head` by default (recorded `pr_head=` is only an offline fallback) before falling back to the local branch with a warning.
175175
For target project repos shipped through their own no-mistakes pipeline, commits under `.no-mistakes/evidence/` are the pipeline's PR-viewable validation evidence and are expected to stay in the crew branch until the evidence-hosting design changes.
176176
The firstmate repo itself is the exception: its `.no-mistakes/` directory is local state, stays gitignored, and is rejected by CI if tracked.
177177
PR-based task merges go through `bin/fm-pr-merge.sh`, which records `pr=` and any available `pr_head=` through `bin/fm-pr-check.sh` before calling `gh-axi pr merge`.

‎docs/scripts.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ The shared no-mistakes gate refusal for fleet lifecycle entrypoints is summarize
4343
| `fm-config-push.sh` | Push declared inherited local material to live secondmate homes mid-session |
4444
| `fm-project-mode.sh` | Resolve a project's delivery mode and `+yolo` flag from `data/projects.md` |
4545
| `fm-merge-local.sh` | Fast-forward a `local-only` project's local default branch after approval |
46-
| `fm-review-diff.sh` | Review a crewmate branch or recorded PR head against the authoritative base |
46+
| `fm-review-diff.sh` | Review a crewmate branch or resolved PR head against the authoritative base |
4747
| `fm-marker-lib.sh` | Shared from-firstmate request marker, detector, and idempotent transformation |
4848
| `fm-gate-refuse-lib.sh` | Shared no-mistakes gate-context refusal for fleet lifecycle entrypoints |
4949
| `fm-watch-arm.sh` | Verified home-scoped watcher arm wrapper with loud cycle endings and bounded lifecycle ledger |

‎tests/fm-review-diff.test.sh‎

Lines changed: 34 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,16 @@
11
#!/usr/bin/env bash
22
# Tests for bin/fm-review-diff.sh: when a task has an open PR recorded in meta,
3-
# the review diff must compare the authoritative base against the PR head, not a
4-
# stale local branch left behind after no-mistakes fix rounds push to the PR.
3+
# the review diff must compare the authoritative base against a freshly fetched
4+
# PR head, not a stale local branch or a stale recorded pr_head= left behind
5+
# after no-mistakes fix rounds push to the PR.
56
#
67
# Matrix:
7-
# (a) pr= + reachable pr_head= -> diff uses PR head, not the lagging local branch
8+
# (a) pr= + reachable pr_head=, no remote pull ref -> offline fallback to recorded SHA
89
# (b) pr= without pr_head= -> fetch refs/pull/<n>/head and diff that
910
# (c) pr= absent -> unchanged worktree-branch diff
1011
# (d) pr= present but PR head unreachable -> fallback to local branch + warning
12+
# (e) pr= + STALE recorded pr_head= + newer remote pull head -> must use fetched head
13+
# (this is the class that bit reviewers holding merges over "missing" fixes)
1114
set -u
1215

1316
# shellcheck source=tests/lib.sh
@@ -76,6 +79,7 @@ test_pr_meta_uses_pr_head_not_stale_local() {
7679
local case_dir out
7780
case_dir=$(make_case pr-head-sha)
7881
stale_and_pr_commits "$case_dir"
82+
# No remote pull ref: fetch fails, recorded pr_head is the offline fallback.
7983
write_task_meta "$case_dir" \
8084
"pr=https://github.com/example/repo/pull/9" \
8185
"pr_head=$PR_SHA"
@@ -85,8 +89,32 @@ test_pr_meta_uses_pr_head_not_stale_local() {
8589
assert_contains "$out" '+pr-fixed' "pr-head-sha: diff should show the PR head content"
8690
assert_not_contains "$out" 'stale-local' "pr-head-sha: diff must not use the stale local branch"
8791
assert_not_contains "$(cat "$case_dir/stderr")" 'warning: PR head unavailable' \
88-
"pr-head-sha: should not warn when pr_head is reachable"
89-
pass "fm-review-diff uses recorded pr_head instead of the lagging local branch"
92+
"pr-head-sha: should not warn when recorded pr_head is reachable offline"
93+
pass "fm-review-diff falls back to recorded pr_head when pull head cannot be fetched"
94+
}
95+
96+
test_stale_recorded_pr_head_loses_to_fetched_pull_head() {
97+
local case_dir out stale_sha
98+
case_dir=$(make_case stale-recorded)
99+
stale_and_pr_commits "$case_dir"
100+
stale_sha=$(git -C "$case_dir/wt" rev-parse fm/task-x1)
101+
# Remote PR head is newer (pipeline fix); meta still points at the older local tip.
102+
git -C "$case_dir/wt" push -q origin "pr-head-tmp:refs/pull/9/head"
103+
write_task_meta "$case_dir" \
104+
"pr=https://github.com/example/repo/pull/9" \
105+
"pr_head=$stale_sha"
106+
107+
out=$(run_review_diff "$case_dir" task-x1 2> "$case_dir/stderr")
108+
109+
assert_contains "$out" '+pr-fixed' \
110+
"stale-recorded: diff must show the fetched PR head, not the recorded stale SHA"
111+
assert_not_contains "$out" 'stale-local' \
112+
"stale-recorded: diff must not use the stale local/recorded content"
113+
assert_not_contains "$(cat "$case_dir/stderr")" 'warning: PR head unavailable' \
114+
"stale-recorded: fetch of refs/pull/<n>/head should succeed"
115+
# Pre-fix behavior preferred reachable recorded pr_head= and would show stale-local.
116+
[ "$stale_sha" != "$PR_SHA" ] || fail "stale-recorded: fixture did not diverge recorded vs PR head"
117+
pass "fm-review-diff prefers freshly fetched PR head over a stale recorded pr_head="
90118
}
91119

92120
test_pr_meta_fetches_pull_head_without_recorded_sha() {
@@ -143,5 +171,6 @@ test_unreachable_pr_head_falls_back_with_warning() {
143171

144172
test_pr_meta_uses_pr_head_not_stale_local
145173
test_pr_meta_fetches_pull_head_without_recorded_sha
174+
test_stale_recorded_pr_head_loses_to_fetched_pull_head
146175
test_no_pr_meta_uses_local_branch
147176
test_unreachable_pr_head_falls_back_with_warning

‎tests/fm-sessionstart-nudge.test.sh‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ set -u
55
# shellcheck source=tests/lib.sh
66
. "$(dirname "${BASH_SOURCE[0]}")/lib.sh"
77

8+
unset NO_MISTAKES_GATE
9+
810
TMP_ROOT=$(fm_test_tmproot fm-sessionstart-nudge)
911
NUDGE="$ROOT/bin/fm-sessionstart-nudge.sh"
1012
NUDGE_LINE="Run \`bin/fm-session-start.sh\` now, exactly once, before executing any other instructions."

0 commit comments

Comments
 (0)