From 999903a3d1acbfce4f9b1b1d90efd8aea76da0b2 Mon Sep 17 00:00:00 2001 From: donpetry-bot <281750570+donpetry-bot@users.noreply.github.com> Date: Fri, 3 Jul 2026 15:49:17 +0000 Subject: [PATCH 1/2] =?UTF-8?q?feat:=20implement=20issue=20#949=20?= =?UTF-8?q?=E2=80=94=20P2=20(#860=20follow-up):=20keep=20agent=20branches?= =?UTF-8?q?=20rebased/squashed=20instead=20of=20merge-stacked?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- scripts/lib/pr-runaway-detect.sh | 40 +++++++++++++++-- scripts/pr_runaway_scan.sh | 14 +++--- tests/test_pr_runaway_detect.bats | 71 +++++++++++++++++++++++++++++++ 3 files changed, 117 insertions(+), 8 deletions(-) diff --git a/scripts/lib/pr-runaway-detect.sh b/scripts/lib/pr-runaway-detect.sh index cf3c103d2..99634e702 100644 --- a/scripts/lib/pr-runaway-detect.sh +++ b/scripts/lib/pr-runaway-detect.sh @@ -36,6 +36,15 @@ set -euo pipefail : "${RUNAWAY_MIN_AGE_HOURS:=48}" # open longer than this, AND churning : "${RUNAWAY_CHURN_MIN_CYCLES:=1}" # min cycles that count as "active churn" +# Merge-stacked (rebase/squash-candidate) smell — issue #949, #860 follow-up. +# #860's branch carried a stale April base and was repeatedly `merge main`-d +# instead of rebased, inflating it to 378 commits for an 18-file net diff. That +# disproportion — a commit count that vastly exceeds the changed-file count — is +# the tell of a branch that should have been rebased/squashed. It fires only when +# BOTH conditions hold (so a genuinely broad PR with many files does not flag). +: "${REBASE_SMELL_MIN_COMMITS:=50}" # > this many commits, AND +: "${REBASE_SMELL_MAX_FILES:=20}" # < this many changed files + # _runaway_int # Echo the value as a non-negative integer, or 0 for empty/non-numeric input. # Mirrors the defensive degradation in pr-automation-budget.sh so a bad metric @@ -96,6 +105,27 @@ is_pr_runaway() { [ -n "$reasons" ] } +# pr_rebase_smell +# Print a single "rebase/squash candidate" reason line when a PR's commit count +# vastly exceeds its changed-file count — the #860 merge-stacking signature (378 +# commits for an 18-file net diff), see #949. Fires only when +# commits > REBASE_SMELL_MIN_COMMITS AND 0 < changed_files < REBASE_SMELL_MAX_FILES. +# The 0-file guard is deliberate: a zero-file diff is an API/data glitch, not a +# merge-stacked branch, and must not false-positive. Empty output otherwise. +pr_rebase_smell() { + local commits files min_commits max_files + commits=$(_runaway_int "${1:-0}") + files=$(_runaway_int "${2:-0}") + min_commits=$(_runaway_threshold "${REBASE_SMELL_MIN_COMMITS}" 50) + max_files=$(_runaway_threshold "${REBASE_SMELL_MAX_FILES}" 20) + + if [ "$commits" -gt "$min_commits" ] && [ "$files" -gt 0 ] && [ "$files" -lt "$max_files" ]; then + printf 'merge-stacked: %s commits / %s files — rebase/squash candidate (>%s commits, <%s files)\n' \ + "$commits" "$files" "$min_commits" "$max_files" + fi + return 0 +} + # pr_age_hours [now_epoch] # Whole hours since created_at. now_epoch defaults to the current time; it is # injectable so callers/tests are deterministic. Unparseable input -> 0. @@ -121,17 +151,21 @@ pr_age_hours() { # all-clear line and no table, so a clean fleet still gets an explicit signal. generate_runaway_report() { local f="${1:-}" - local max_commits max_comments max_cycles min_age + local max_commits max_comments max_cycles min_age smell_commits smell_files max_commits=$(_runaway_threshold "${RUNAWAY_MAX_COMMITS}" 50) max_comments=$(_runaway_threshold "${RUNAWAY_MAX_COMMENTS}" 200) max_cycles=$(_runaway_threshold "${RUNAWAY_MAX_CYCLES}" 10) min_age=$(_runaway_threshold "${RUNAWAY_MIN_AGE_HOURS}" 48) + smell_commits=$(_runaway_threshold "${REBASE_SMELL_MIN_COMMITS}" 50) + smell_files=$(_runaway_threshold "${REBASE_SMELL_MAX_FILES}" 20) printf '## Runaway PR Candidates\n\n' printf 'Open PRs exceeding a soft runaway threshold ' - printf '(commits >%s, comments >%s, automated cycles >%s, or open >%sh with agent churn). ' \ + printf '(commits >%s, comments >%s, automated cycles >%s, open >%sh with agent churn, ' \ "$max_commits" "$max_comments" "$max_cycles" "$min_age" - printf 'Detection only — no PR is mutated. See #948 / the #860 post-mortem.\n\n' + printf 'or merge-stacked: >%s commits with <%s files — a rebase/squash candidate). ' \ + "$smell_commits" "$smell_files" + printf 'Detection only — no PR is mutated. See #948 / #949 / the #860 post-mortem.\n\n' if [ -z "$f" ] || [ ! -s "$f" ]; then printf '✅ No open PR crossed a runaway threshold.\n' diff --git a/scripts/pr_runaway_scan.sh b/scripts/pr_runaway_scan.sh index ba96c4831..f89bb4f14 100644 --- a/scripts/pr_runaway_scan.sh +++ b/scripts/pr_runaway_scan.sh @@ -33,7 +33,7 @@ TODAY=$(date -u +%Y-%m-%d) echo "=== Runaway-PR Detection — Daily Scan ===" echo " Repo: $REPO" echo " Date: $TODAY" -echo " Thresholds: commits>${RUNAWAY_MAX_COMMITS} comments>${RUNAWAY_MAX_COMMENTS} cycles>${RUNAWAY_MAX_CYCLES} age>${RUNAWAY_MIN_AGE_HOURS}h(+churn)" +echo " Thresholds: commits>${RUNAWAY_MAX_COMMITS} comments>${RUNAWAY_MAX_COMMENTS} cycles>${RUNAWAY_MAX_CYCLES} age>${RUNAWAY_MIN_AGE_HOURS}h(+churn) merge-stacked(>${REBASE_SMELL_MIN_COMMITS} commits,<${REBASE_SMELL_MAX_FILES} files)" echo "" # --------------------------------------------------------------------------- @@ -81,10 +81,10 @@ while IFS= read -r pr; do [ -n "$pr" ] || continue scanned=$(( scanned + 1 )) - # Single detail call gives commit/comment counts + created_at + link + title. + # Single detail call gives commit/comment/file counts + created_at + link + title. detail=$(gh api "repos/${REPO}/pulls/${pr}" \ - --jq '{commits, comments, created_at, html_url, title}' 2>/dev/null || echo '{}') - IFS=$'\t' read -r commits comments created_at html_url title <<< "$(jq -r '[.commits // 0, .comments // 0, .created_at // "", .html_url // "", (.title // "" | tostring)] | @tsv' <<< "$detail")" + --jq '{commits, comments, changed_files, created_at, html_url, title}' 2>/dev/null || echo '{}') + IFS=$'\t' read -r commits comments changed_files created_at html_url title <<< "$(jq -r '[.commits // 0, .comments // 0, .changed_files // 0, .created_at // "", .html_url // "", (.title // "" | tostring)] | @tsv' <<< "$detail")" # Automated cycles since the last human — reuse the #926 budget computation so # the two guards agree on what "an automated cycle" means. @@ -93,7 +93,11 @@ while IFS= read -r pr; do age_hours=$(pr_age_hours "$created_at" "$now_epoch") - reasons=$(pr_runaway_reasons "$commits" "$comments" "$cycles" "$age_hours") + # Combine the runaway thresholds (#948) with the merge-stacked rebase/squash + # smell (#949); sed drops the blank line when either half is empty. + reasons=$(printf '%s\n%s\n' \ + "$(pr_runaway_reasons "$commits" "$comments" "$cycles" "$age_hours")" \ + "$(pr_rebase_smell "$commits" "$changed_files")" | sed '/^$/d') if [ -n "$reasons" ]; then # Sanitize the title for a single markdown table cell: strip newlines/tabs # (the TSV delimiter) and pipes (the markdown column delimiter). diff --git a/tests/test_pr_runaway_detect.bats b/tests/test_pr_runaway_detect.bats index 72876eb4d..cc58befa4 100644 --- a/tests/test_pr_runaway_detect.bats +++ b/tests/test_pr_runaway_detect.bats @@ -125,6 +125,77 @@ teardown() { [ "$n" -eq 4 ] } +# --------------------------------------------------------------------------- +# pr_rebase_smell — merge-stacked (rebase/squash candidate) detector (#949) +# Fires when commits > REBASE_SMELL_MIN_COMMITS (50) AND +# 0 < changed_files < REBASE_SMELL_MAX_FILES (20) — the #860 signature of a +# branch merge-stacked instead of rebased (378 commits for an 18-file diff). +# --------------------------------------------------------------------------- + +@test "the #860 signature (378 commits / 18 files) fires the rebase smell" { + run pr_rebase_smell 378 18 + [ "$status" -eq 0 ] + [[ "$output" == *merge-stacked* ]] + [[ "$output" == *rebase* ]] + [[ "$output" == *378* ]] + [[ "$output" == *18* ]] +} + +@test "commits at the smell floor do NOT fire (strict >)" { + # 50 commits is not > 50 + run pr_rebase_smell 50 5 + [ -z "$output" ] +} + +@test "changed_files at the smell ceiling do NOT fire (strict <)" { + # 20 files is not < 20 + run pr_rebase_smell 100 20 + [ -z "$output" ] +} + +@test "a big-but-broad PR (many commits, many files) does NOT fire" { + # 60 commits across 60 files is proportionate work, not merge-stacking. + run pr_rebase_smell 60 60 + [ -z "$output" ] +} + +@test "a zero-file diff does NOT fire (data glitch, not merge-stacking)" { + run pr_rebase_smell 300 0 + [ -z "$output" ] +} + +@test "REBASE_SMELL_MIN_COMMITS override is respected" { + REBASE_SMELL_MIN_COMMITS=10 run pr_rebase_smell 11 3 + [[ "$output" == *merge-stacked* ]] + REBASE_SMELL_MIN_COMMITS=10 run pr_rebase_smell 10 3 + [ -z "$output" ] +} + +@test "REBASE_SMELL_MAX_FILES override is respected" { + REBASE_SMELL_MAX_FILES=5 run pr_rebase_smell 100 4 + [[ "$output" == *merge-stacked* ]] + REBASE_SMELL_MAX_FILES=5 run pr_rebase_smell 100 5 + [ -z "$output" ] +} + +@test "rebase smell degrades to no-fire on non-numeric/empty input" { + run pr_rebase_smell "" "abc" + [ "$status" -eq 0 ] + [ -z "$output" ] + run pr_rebase_smell + [ "$status" -eq 0 ] + [ -z "$output" ] +} + +@test "generate_runaway_report documents the merge-stacked criterion" { + local f + f=$(mktemp "$STUB_BIN_DIR/tsv.XXXXXX") + : > "$f" + run generate_runaway_report "$f" + [ "$status" -eq 0 ] + [[ "$output" == *merge-stacked* ]] +} + # --------------------------------------------------------------------------- # is_pr_runaway — boolean wrapper # --------------------------------------------------------------------------- From a2f7e0578dfd1c54b6d55cfddcf794d076dd883e Mon Sep 17 00:00:00 2001 From: donpetry-bot <281750570+donpetry-bot@users.noreply.github.com> Date: Fri, 3 Jul 2026 19:48:54 +0000 Subject: [PATCH 2/2] chore: dev-lead update (review-changes) [skip ci-relay] --- scripts/pr_runaway_scan.sh | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/scripts/pr_runaway_scan.sh b/scripts/pr_runaway_scan.sh index f89bb4f14..73a4e8836 100644 --- a/scripts/pr_runaway_scan.sh +++ b/scripts/pr_runaway_scan.sh @@ -94,10 +94,15 @@ while IFS= read -r pr; do age_hours=$(pr_age_hours "$created_at" "$now_epoch") # Combine the runaway thresholds (#948) with the merge-stacked rebase/squash - # smell (#949); sed drops the blank line when either half is empty. - reasons=$(printf '%s\n%s\n' \ - "$(pr_runaway_reasons "$commits" "$comments" "$cycles" "$age_hours")" \ - "$(pr_rebase_smell "$commits" "$changed_files")" | sed '/^$/d') + # smell (#949) in pure bash — avoids a per-PR subshell + sed fork. + reasons_runaway=$(pr_runaway_reasons "$commits" "$comments" "$cycles" "$age_hours") + reasons_smell=$(pr_rebase_smell "$commits" "$changed_files") + if [ -n "$reasons_runaway" ] && [ -n "$reasons_smell" ]; then + reasons="${reasons_runaway} +${reasons_smell}" + else + reasons="${reasons_runaway:-$reasons_smell}" + fi if [ -n "$reasons" ]; then # Sanitize the title for a single markdown table cell: strip newlines/tabs # (the TSV delimiter) and pipes (the markdown column delimiter).