| 1 | #!/usr/bin/env bash |
| 2 | # Report-only complexity monitor for internal/agent production code. |
| 3 | # Never fails the job (issues-exit-code=0). Used to track funlen/cyclop |
| 4 | # trends after the Run/executeOne extraction without duplicating the main |
| 5 | # golangci-lint quality gate. |
| 6 | set -euo pipefail |
| 7 | |
| 8 | ROOT="$(cd "$(dirname "$0")/.." && pwd)" |
| 9 | cd "$ROOT" |
| 10 | |
| 11 | CONFIG="${AGENT_COMPLEXITY_CONFIG:-.golangci-agent-complexity.yml}" |
| 12 | OUT_DIR="${AGENT_COMPLEXITY_OUT:-.}" |
| 13 | REPORT="${OUT_DIR}/agent-complexity-report.txt" |
| 14 | SUMMARY_FILE="${GITHUB_STEP_SUMMARY:-}" |
| 15 | |
| 16 | mkdir -p "$(dirname "$REPORT")" |
| 17 | |
| 18 | echo "Agent complexity report (report-only, non-blocking)" |
| 19 | echo "config=${CONFIG}" |
| 20 | echo "scope=./internal/agent (tests excluded)" |
| 21 | echo |
| 22 | |
| 23 | set +e |
| 24 | # Match the pinned CI golangci-lint version when available; fall back to PATH. |
| 25 | if command -v golangci-lint >/dev/null 2>&1; then |
| 26 | golangci-lint run \ |
| 27 | --config "$CONFIG" \ |
| 28 | --issues-exit-code=0 \ |
| 29 | --timeout=5m \ |
| 30 | ./internal/agent/... >"$REPORT" 2>&1 |
| 31 | status=$? |
| 32 | else |
| 33 | echo "golangci-lint not found on PATH; skipping complexity report" | tee "$REPORT" |
| 34 | status=0 |
| 35 | fi |
| 36 | set -e |
| 37 | |
| 38 | # Always surface the report. |
| 39 | cat "$REPORT" |
| 40 | echo |
| 41 | |
| 42 | # Summarize counts for humans and CI step summaries. |
| 43 | cyclop_count=$(grep -c '(cyclop)$' "$REPORT" 2>/dev/null || true) |
| 44 | funlen_count=$(grep -c '(funlen)$' "$REPORT" 2>/dev/null || true) |
| 45 | # grep -c exits 1 on zero matches under set -e; coerce empty to 0. |
| 46 | cyclop_count=${cyclop_count:-0} |
| 47 | funlen_count=${funlen_count:-0} |
| 48 | total=$((cyclop_count + funlen_count)) |
| 49 | |
| 50 | # Extract the highest reported cyclomatic complexity if present. |
| 51 | max_complexity=$(grep -oE 'complexity for function [^ ]+ is [0-9]+' "$REPORT" 2>/dev/null \ |
| 52 | | grep -oE '[0-9]+$' \ |
| 53 | | sort -nr \ |
| 54 | | head -1 || true) |
| 55 | max_complexity=${max_complexity:-0} |
| 56 | |
| 57 | # Extract the longest statement count if present. |
| 58 | max_statements=$(grep -oE 'too many statements \([0-9]+' "$REPORT" 2>/dev/null \ |
| 59 | | grep -oE '[0-9]+' \ |
| 60 | | sort -nr \ |
| 61 | | head -1 || true) |
| 62 | max_statements=${max_statements:-0} |
| 63 | |
| 64 | summary=$(cat <<EOF |
| 65 | ### Agent complexity monitor (report-only) |
| 66 | |
| 67 | | Metric | Value | |
| 68 | | --- | ---: | |
| 69 | | cyclop findings | ${cyclop_count} | |
| 70 | | funlen findings | ${funlen_count} | |
| 71 | | total findings | ${total} | |
| 72 | | max cyclomatic complexity | ${max_complexity} | |
| 73 | | max statements (funlen) | ${max_statements} | |
| 74 | |
| 75 | Scope: \`internal/agent\` production code only (tests excluded). |
| 76 | Mode: report-only (\`issues-exit-code=0\`). Does not fail CI. |
| 77 | Thresholds: funlen lines=120 / statements=80, cyclop max=30. |
| 78 | Future work: block only new violations relative to this baseline. |
| 79 | EOF |
| 80 | ) |
| 81 | |
| 82 | echo "$summary" |
| 83 | if [[ -n "$SUMMARY_FILE" ]]; then |
| 84 | echo "$summary" >>"$SUMMARY_FILE" |
| 85 | fi |
| 86 | |
| 87 | # Never block: surface tool failures as warnings only. |
| 88 | if [[ "$status" -ne 0 ]]; then |
| 89 | echo "::warning::agent complexity report command exited with status ${status} (report-only; not failing CI)" |
| 90 | fi |
| 91 | exit 0 |
| 92 |