Skip to content
Merged
40 changes: 37 additions & 3 deletions scripts/lib/pr-runaway-detect.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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 <value>
# 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
Expand Down Expand Up @@ -96,6 +105,27 @@ is_pr_runaway() {
[ -n "$reasons" ]
}

# pr_rebase_smell <commits> <changed_files>
# 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 <created_at_iso8601> [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.
Expand All @@ -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'
Expand Down
19 changes: 14 additions & 5 deletions scripts/pr_runaway_scan.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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 ""

# ---------------------------------------------------------------------------
Expand Down Expand Up @@ -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.
Expand All @@ -93,7 +93,16 @@ 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) 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).
Expand Down
71 changes: 71 additions & 0 deletions tests/test_pr_runaway_detect.bats
Original file line number Diff line number Diff line change
Expand Up @@ -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
# ---------------------------------------------------------------------------
Expand Down
Loading