#!/usr/bin/env bash # gstack-retro-metrics — /retro's metric pipelines, consolidated (token-reduction). # # Absorbs the inline git/awk pipelines retro/SKILL.md used to carry in Steps # 0.5-9 and 11 (~13KB of fences in every install). The skill now runs ONE # fence; this script emits labeled `METRIC_NAME: value` lines the prose # interprets. The contract is pinned by test/gstack-retro-metrics.test.ts. # # LOCAL READS ONLY — no network ops of any kind. The freshness fetch stays in # the skill's own Step 0.5 fence (skill prose, same as before this script # existed), so this script never needs egress receipts. # # Conventions mirror bin/gstack-skill-start: # - Paths resolve $0-relative (works for every host + install layout). # - State paths honor ${GSTACK_HOME:-$HOME/.gstack}. # - Error style: per-line `|| true`, never `set -e` — a mid-script failure # must not drop later METRIC lines. # - No heredocs (nothing to BASH_COMPAT-guard; see # test/heredoc-pipe-deadlock.test.ts if one is ever added). # # Usage: # gstack-retro-metrics --base --since \ # [--until ] # # --base the detected default branch (from BASE_BRANCH_DETECT). The script # prefers origin/, falls back to the local (local-only # repos), then HEAD. The ref actually used is echoed as RETRO_REF. # --since midnight-aligned ISO date ("2026-03-11T00:00:00") or a relative # expression ("24 hours ago"). Default: "7 days ago". # --until optional window end (compare mode's prior window). BASE="" SINCE="7 days ago" UNTIL="" while [ $# -gt 0 ]; do case "$1" in --base) BASE="$2"; shift 2 ;; --since) SINCE="$2"; shift 2 ;; --until) UNTIL="$2"; shift 2 ;; *) shift ;; esac done # $0-relative resolution (parity with gstack-skill-start; any future # sibling-bin call must go through $_BIN, never bare PATH lookup). _SCRIPT_DIR=$(cd "$(dirname "$0")" 2>/dev/null && pwd) _BIN="$_SCRIPT_DIR" _GH="${GSTACK_HOME:-$HOME/.gstack}" echo "RETRO_METRICS_PROTO: 1" if ! git rev-parse --git-dir >/dev/null 2>&1; then echo "RETRO_METRICS_ERROR: not inside a git repository" exit 0 fi # ── Guard + ref resolution (Step 0.5's local checks; the fetch stays in prose) ─ _HAS_ORIGIN=$(git remote 2>/dev/null | grep -c '^origin$' || true) case "$_HAS_ORIGIN" in ''|*[!0-9]*) _HAS_ORIGIN=0 ;; esac [ "$_HAS_ORIGIN" -gt 0 ] && echo "GUARD_REMOTE: origin" || echo "GUARD_REMOTE: none" _HEAD_REF=$(git symbolic-ref --quiet --short HEAD 2>/dev/null || true) [ -n "$_HEAD_REF" ] && echo "GUARD_HEAD: $_HEAD_REF" || echo "GUARD_HEAD: detached" if [ -z "$BASE" ]; then BASE=$(git symbolic-ref --quiet refs/remotes/origin/HEAD 2>/dev/null | sed 's|^refs/remotes/origin/||') fi if [ -z "$BASE" ]; then for _CAND in main master; do if git rev-parse --verify --quiet "refs/heads/$_CAND" >/dev/null 2>&1 \ || git rev-parse --verify --quiet "refs/remotes/origin/$_CAND" >/dev/null 2>&1; then BASE="$_CAND"; break fi done fi [ -n "$BASE" ] || BASE="$_HEAD_REF" REF="HEAD" if [ -n "$BASE" ] && git rev-parse --verify --quiet "refs/remotes/origin/$BASE" >/dev/null 2>&1; then REF="origin/$BASE" elif [ -n "$BASE" ] && git rev-parse --verify --quiet "refs/heads/$BASE" >/dev/null 2>&1; then REF="$BASE" fi echo "RETRO_REF: $REF" _LATEST=$(git log -1 --format=%ci "$REF" 2>/dev/null | cut -d' ' -f1) echo "GUARD_LATEST_COMMIT: ${_LATEST:-unknown}" echo "WINDOW_SINCE: $SINCE" echo "WINDOW_UNTIL: ${UNTIL:-(now)}" _USER_NAME=$(git config user.name 2>/dev/null || true) _USER_EMAIL=$(git config user.email 2>/dev/null || true) echo "USER_NAME: ${_USER_NAME:-unknown}" echo "USER_EMAIL: ${_USER_EMAIL:-unknown}" # Window args, applied uniformly to every windowed git query below. _S="--since=$SINCE" _U="" [ -n "$UNTIL" ] && _U="--until=$UNTIL" # ── Main pass: one numstat walk computes the whole per-commit metric family ── # Emits COMMIT: lines (newest first, capped) plus every aggregate. Subjects may # contain '|', so fields are re-joined from index 6 on. Test-file detection is # the union of the historical patterns (dir-based test/|spec/|__tests__/ and # suffix-based .test./.spec./_test./_spec.). git log "$REF" "$_S" ${_U:+"$_U"} --date=format-local:'%Y-%m-%d %H:%M' \ --format='C|%h|%aN|%at|%ad|%s' --numstat 2>/dev/null | awk ' function is_test(p) { return (p ~ /(^|\/)(tests?|spec|__tests__)\//) || (p ~ /(\.(test|spec)\.|_test\.|_spec\.)/) } function type_of(s) { if (s ~ /^Merge /) return "merge" sub(/^v[0-9][0-9.]* /, "", s) # squash-merge convention: "v1.2.3.0 fix: ..." if (match(s, /^(feat|fix|refactor|test|chore|docs|perf|style|build|ci|revert)(\(|!|:)/)) { t = substr(s, 1, RLENGTH - 1); sub(/[(!:]$/, "", t); return t } return "other" } function bucket_of(loc) { if (loc < 100) return "small"; if (loc < 500) return "medium" if (loc < 1500) return "large"; return "xl" } function flush() { if (h == "") return commits++ ins += cins; del += cdel; tins += ctins files_sum = (cfiles > 20 ? 20 : cfiles); weighted += files_sum t = type_of(subj); types[t]++; atypes[author "|" t]++ if (t == "merge") merges++ hour = substr(dt, 12, 2); hours[hour]++; ahours[author "|" hour]++ day = substr(dt, 1, 10); days[day] = 1 wk = int((anchor - at) / 604800) wcommits[wk]++; wins[wk] += cins; wdel[wk] += cdel; wtins[wk] += ctins if (wk > maxwk) maxwk = wk acommits[author]++; ains[author] += cins; adel[author] += cdel; atins[author] += ctins loc = cins + cdel; sizes[bucket_of(loc)]++ if (loc > bigloc) { bigloc = loc; bigline = h "|" loc "|" author "|" subj } if (loc > abigloc[author]) { abigloc[author] = loc; abig[author] = h "|" loc "|" subj } # 45-minute session gaps (walked newest→oldest; equivalent to ascending). if (prev_at == 0) { sess = 1; sess_end = at; } else if (prev_at - at > 2700) { sess_dur = (sess_end - sess_start_at) / 60; classify(sess_dur) sess++; sess_end = at } sess_start_at = at; prev_at = at if (commits <= 300) printf "COMMIT: %s|%s|%s|+%d/-%d|%s\n", h, author, dt, cins, cdel, subj h = "" } function classify(mins) { total_mins += mins if (mins >= 50) deep++; else if (mins >= 20) medium++; else micro++ } /^C\|/ { flush() n = split($0, a, "|") h = a[2]; author = a[3]; at = a[4] + 0; dt = a[5] subj = a[6]; for (i = 7; i <= n; i++) subj = subj "|" a[i] if (anchor == 0) anchor = at cins = 0; cdel = 0; ctins = 0; cfiles = 0 next } /^[0-9-]+\t/ { if ($1 != "-") cins += $1; if ($2 != "-") cdel += $2 if ($1 != "-" && is_test($3)) ctins += $1 cfiles++ filecount[$3]++ d = ($3 ~ /\//) ? substr($3, 1, index($3, "/") - 1) "/" : "(root)" dircount[d]++; adir[author "|" d]++ if ($3 ~ /(\.(test|spec)\.|_test\.|_spec\.)/) testfiles[$3] = 1 } END { flush() if (commits > 0 && prev_at != 0) { sess_dur = (sess_end - sess_start_at) / 60; classify(sess_dur) } if (commits > 300) printf "COMMIT_LIST_TRUNCATED: showing 300 of %d (newest first; git log for the rest)\n", commits printf "COMMITS: %d\n", commits printf "MERGE_COMMITS: %d\n", merges nauth = 0; for (au in acommits) nauth++ printf "CONTRIBUTORS: %d\n", nauth printf "INSERTIONS: %d\n", ins printf "DELETIONS: %d\n", del printf "NET_LOC: %d\n", ins - del printf "TEST_INSERTIONS: %d\n", tins printf "TEST_RATIO: %s\n", (ins > 0 ? sprintf("%d%%", tins * 100 / ins) : "n/a") printf "WEIGHTED_COMMITS: %d\n", weighted nd = 0; for (d in days) nd++ printf "ACTIVE_DAYS: %d\n", nd ntf = 0; for (f in testfiles) ntf++ printf "TEST_FILES_CHANGED: %d\n", ntf printf "SESSIONS: %d\n", sess printf "DEEP_SESSIONS: %d\n", deep printf "MEDIUM_SESSIONS: %d\n", medium printf "MICRO_SESSIONS: %d\n", micro printf "TOTAL_ACTIVE_MINUTES: %d\n", total_mins printf "AVG_SESSION_MINUTES: %d\n", (sess > 0 ? total_mins / sess : 0) if (total_mins >= 5) printf "LOC_PER_SESSION_HOUR: %d\n", int(ins / (total_mins / 60) / 50 + 0.5) * 50 else printf "LOC_PER_SESSION_HOUR: n/a (too little session time)\n" line = "" for (t in types) line = line (line == "" ? "" : " ") t "=" types[t] printf "COMMIT_TYPES: %s\n", (line == "" ? "none" : line) printf "FIX_RATIO: %s\n", (commits > 0 ? sprintf("%d%%", types["fix"] * 100 / commits) : "n/a") printf "COMMIT_SIZE_BUCKETS: small=%d medium=%d large=%d xl=%d\n", sizes["small"], sizes["medium"], sizes["large"], sizes["xl"] # Hour histogram (nonzero hours only, chronological). line = "" for (i = 0; i < 24; i++) { hh = sprintf("%02d", i); if (hours[hh] > 0) line = line (line == "" ? "" : " ") hh "=" hours[hh] } printf "HOURS: %s\n", (line == "" ? "none" : line) peak = ""; pc = -1 for (hh in hours) if (hours[hh] > pc) { pc = hours[hh]; peak = hh } printf "PEAK_HOUR: %s\n", (peak == "" ? "n/a" : peak) # Focus score: share of file changes in the single busiest top-level dir. tot = 0; for (d in dircount) tot += dircount[d] fd = ""; fc = -1 for (d in dircount) if (dircount[d] > fc) { fc = dircount[d]; fd = d } if (tot > 0) printf "FOCUS_SCORE: %d%% (%s)\n", fc * 100 / tot, fd else printf "FOCUS_SCORE: n/a\n" if (bigline != "") printf "BIGGEST_COMMIT: %s\n", bigline # Top-10 hotspots by change count. for (k = 0; k < 10; k++) { bf = ""; bc = 0 for (f in filecount) if (filecount[f] > bc) { bc = filecount[f]; bf = f } if (bf == "") break printf "HOTSPOT: %d %s\n", bc, bf delete filecount[bf] } # Per-author lines, sorted by commits desc (selection sort — small n). while (1) { ba = ""; bc = -1 for (au in acommits) if (!(au in done) && acommits[au] > bc) { bc = acommits[au]; ba = au } if (ba == "") break done[ba] = 1 tr = (ains[ba] > 0 ? sprintf("%d%%", atins[ba] * 100 / ains[ba]) : "n/a") # top-3 areas for this author areas = "" for (k = 0; k < 3; k++) { bd = ""; bdc = 0 for (key in adir) { split(key, kk, "|") if (kk[1] == ba && !((key) in adone) && adir[key] > bdc) { bdc = adir[key]; bd = key } } if (bd == "") break adone[bd] = 1 split(bd, kk, "|") areas = areas (areas == "" ? "" : ",") kk[2] } tl = "" for (key in atypes) { split(key, kk, "|"); if (kk[1] == ba) tl = tl (tl == "" ? "" : ",") kk[2] ":" atypes[key] } ph = ""; phc = -1 for (key in ahours) { split(key, kk, "|"); if (kk[1] == ba && ahours[key] > phc) { phc = ahours[key]; ph = kk[2] } } printf "AUTHOR: %s|commits=%d|ins=%d|del=%d|test_ratio=%s|top_areas=%s|types=%s|peak_hour=%s\n", ba, acommits[ba], ains[ba], adel[ba], tr, areas, tl, ph if (abig[ba] != "") printf "AUTHOR_BIGGEST: %s|%s\n", ba, abig[ba] } # Weekly buckets, newest week first (w0 = week containing the newest commit). for (w = 0; w <= maxwk; w++) { if (wcommits[w] == 0) continue wr = (wins[w] > 0 ? sprintf("%d%%", wtins[w] * 100 / wins[w]) : "n/a") printf "WEEK: w%d|commits=%d|ins=%d|del=%d|test_ratio=%s\n", w, wcommits[w], wins[w], wdel[w], wr } } ' || true # ── Co-author trailers: AI-assist count + human co-author credit lines ────── git log "$REF" "$_S" ${_U:+"$_U"} \ --format='%h %(trailers:key=Co-Authored-By,valueonly,separator=;)' 2>/dev/null | awk ' { if (NF < 2) next rest = substr($0, index($0, " ") + 1) n = split(rest, tr, ";") for (i = 1; i <= n; i++) { t = tr[i] if (t ~ /^[ \t]*$/) continue if (tolower(t) ~ /(claude|copilot|codex|gemini|gpt|anthropic|openai|cursor|devin|\[bot\])/) { ai[$1] = 1 } else { human++; if (human <= 40) printf "COAUTHOR: %s|%s\n", $1, t } } } END { if (human > 40) printf "COAUTHOR_LIST_TRUNCATED: showing 40 of %d\n", human c = 0; for (h in ai) c++ printf "AI_ASSISTED_COMMITS: %d\n", c } ' || true # ── Logical SLOC added: non-blank, non-comment added lines in the window ──── _LSLOC=$(git log "$REF" "$_S" ${_U:+"$_U"} -p --format= 2>/dev/null | awk ' /^\+/ && !/^\+\+\+/ { l = substr($0, 2); gsub(/^[ \t]+|[ \t]+$/, "", l) if (l == "") next if (l ~ /^(\/\/|#|\*|\/\*|