fix(review-loop-run): counts parses count-by-severity block with scope + format branches (2026-09-13 fix, 40/40 tests)

Was live via ~/sbin symlink but never committed. Baseline for bug 5352fe72.

Claude-Session: https://claude.ai/code/session_01G3SkMRBRWXyiGuMGs8WVas
This commit is contained in:
Paul O'Reilly
2026-09-16 14:27:52 +12:00
parent 636b8b2401
commit b340e2cd5c
8 changed files with 152 additions and 11 deletions

View File

@@ -39,24 +39,80 @@ report_dir_for() {
echo "${REVIEW_LOOP_RUN_DIR:-${XDG_RUNTIME_DIR:-/tmp}/review-loop/$(basename "$repo")}"
}
# _counts_scope <file> → prints the text of the last "count by severity"
# section (heading matching /count.*severity|severity.*count|summary/i, up
# to the next heading or EOF), or the last 40 lines if no such heading exists.
_counts_scope() {
local f="$1"
local hdr_line
hdr_line=$(grep -inE 'count.*severity|severity.*count|summary' "$f" | tail -1 | cut -d: -f1) || true
if [[ -n "$hdr_line" ]]; then
awk -v start="$hdr_line" '
NR==start { found=1 }
found && NR>start && /^#{1,4}[[:space:]]/ { exit }
found { print }
' "$f"
else
tail -n 40 "$f"
fi
}
# _counts_extract <scope-text> <sev> → last matching value for SEV in scope,
# accepting: "SEV: n", "SEV = n", "- SEV: n", "**SEV**: n", "| SEV | n |",
# "SEV (n)", "n SEV". Empty if none found.
_counts_extract() {
local scope="$1" sev="$2" line n
# Strip markdown emphasis markers (bold/italic) — they can appear before
# or after the colon (e.g. "**SEV:**" as well as "**SEV**:").
local clean; clean=$(sed 's/\*//g' <<<"$scope")
line=$(grep -inE "\\| *${sev} *\\| *[0-9]+ *\\||${sev}[[:space:]]*[:=][[:space:]]*[0-9]+|${sev}[[:space:]]*\\([0-9]+\\)|[0-9]+[[:space:]]+${sev}\\b" <<<"$clean" | tail -1) || true
[[ -z "$line" ]] && { echo ""; return; }
if [[ "$line" =~ \|[[:space:]]*${sev}[[:space:]]*\|[[:space:]]*([0-9]+)[[:space:]]*\| ]]; then
n="${BASH_REMATCH[1]}"
elif [[ "$line" =~ ${sev}[[:space:]]*[:=][[:space:]]*([0-9]+) ]]; then
n="${BASH_REMATCH[1]}"
elif [[ "$line" =~ ${sev}[[:space:]]*\(([0-9]+)\) ]]; then
n="${BASH_REMATCH[1]}"
elif [[ "$line" =~ ([0-9]+)[[:space:]]+${sev} ]]; then
n="${BASH_REMATCH[1]}"
fi
echo "${n:-}"
}
# counts <file> → prints CRITICAL=n HIGH=n MEDIUM=n LOW=n ; exit 4 if C+H>0
# Parses the review's own "count by severity" block first (last such block in
# the file); only falls back to counting finding headings (marked "(derived)")
# when no count block yields any severity value at all.
counts() {
local f="$1"
[[ -f "$f" ]] || die "result file not found: $f" 2
shopt -s nocasematch
local scope; scope=$(_counts_scope "$f")
local -A vals=()
local sev found_any=0
for sev in CRITICAL HIGH MEDIUM LOW; do
local n; n=$(_counts_extract "$scope" "$sev")
if [[ -n "$n" ]]; then vals[$sev]="$n"; found_any=1; fi
done
local derived=""
if [[ "$found_any" -eq 0 ]]; then
derived=" (derived)"
for sev in CRITICAL HIGH MEDIUM LOW; do
local n
n=$(grep -cE "^[[:space:]]*(#+[[:space:]]*)?(\*\*)?${sev}(\*\*)?[[:space:]]*(:|—|-)" "$f") || n=0
vals[$sev]="$n"
done
fi
shopt -u nocasematch
local out="" total=0
for sev in CRITICAL HIGH MEDIUM LOW; do
local n
n=$(grep -iE "^\| *${sev} *\| *[0-9]+" "$f" | head -1 | sed -E 's/^\| *[A-Za-z]+ *\| *([0-9]+).*/\1/') || true
if [[ -z "$n" ]]; then
n=$(grep -iE "^[-*]* *\**${sev}:?\**:? *\**([0-9]+)" "$f" | head -1 | sed -E 's/.*[^0-9]([0-9]+)[^0-9]*$/\1/') || true
fi
if [[ -z "$n" ]]; then
n=$(grep -cE "^#{2,4} .*\b${sev}\b" "$f") || true
fi
n="${n:-0}"; out+="${sev}=${n} "
local n="${vals[$sev]:-0}"
out+="${sev}=${n} "
if [[ "$sev" == "CRITICAL" || "$sev" == "HIGH" ]]; then total=$((total + n)); fi
done
echo "$out"
echo "${out% }${derived}"
if [[ "$total" -gt 0 ]]; then return 4; fi
return 0
}