#!/usr/bin/env bash # gstack-review-read — read review log and config for dashboard # Usage: gstack-review-read # # Emits, in order: reviews JSONL with computed review_freshness, ---CONFIG--- (skip_eng_review), # ---HEAD--- (short sha), ---WTREE--- (current working-tree fingerprint from # bin/gstack-wtree --review, or "unknown"), ---TREE--- (HEAD tree, informational) and # ---DIRTY--- (tracked-file dirty flag). Consumers grade diff-scoped review # rows using review_freshness; legacy or uncaptured diff evidence cannot fall # back to HEAD equality. Plan rows are unchanged. A gate finding the owner's gstack-approvals # gate row closes reads `kept` with its `disposition`, and the row's `gate_dispositions` lists # those approval ids (lib/gate-dispositions.ts). review_freshness counts the owner's # gstack-approvals rule rows at a head in the row's range (ruledOn), and the row's `ruled_notes` # lists their approval ids; values the row itself carried are discarded. When a stopped pass stays # UNVERIFIED, its reason names the findings no ruling covers yet (rulingGap, GST-3267), or says the row # lists no findings at all. A stopped row logged with --findings shows that file's findings, and a native # row that lists none shows its pass's review-row findings, with `findings_from` naming the source # (withCollectedFindings, GST-3296). A row read on other content stays CURRENT when HEAD is its commit plus clean merges of # the base with the branch's patch unchanged, and its reason names the reviewed base (baseMergeOnly, GST-3308). set -euo pipefail GSTACK_BUN_CONFIG=/dev/null; case "${OSTYPE:-}" in msys*|cygwin*) if command -v cygpath >/dev/null 2>&1; then GSTACK_BUN_CONFIG=NUL; fi ;; esac SCRIPT_DIR="$(cd "$(dirname "$0")" && pwd)" case "$(uname -s)" in MINGW*|MSYS*|CYGWIN*) command -v cygpath >/dev/null 2>&1 && SCRIPT_DIR="$(cygpath -m "$SCRIPT_DIR")" ;; esac eval "$("$SCRIPT_DIR/gstack-slug" 2>/dev/null)" SLUG=$("$SCRIPT_DIR/gstack-slug" --get REPO_SLUG 2>/dev/null || true) . "$(dirname "$0")/gstack-state-root.sh" 2>/dev/null || { echo "$0: cannot resolve the gstack state root: $(dirname "$0")/gstack-state-root.sh is missing. fix: reinstall with ./setup or /gstack-upgrade (docs/state-root.md)" >&2; exit 1; } gstack_state_root_select GSTACK_HOME="$_gstack_sr_root" WTREE=$("$SCRIPT_DIR/gstack-wtree" --review 2>/dev/null || echo "unknown") if [ -f "$GSTACK_HOME/projects/$SLUG/$BRANCH-reviews.jsonl" ]; then GSTACK_REVIEW_LIB="$SCRIPT_DIR/../lib/review-evidence.ts" GSTACK_PATCH_LIB="$SCRIPT_DIR/../lib/ship-receipt-patch.ts" GSTACK_POLICY_LIB="$SCRIPT_DIR/../lib/ship-policy.ts" GSTACK_GATE_LIB="$SCRIPT_DIR/../lib/gate-dispositions.ts" GSTACK_RULED_LIB="$SCRIPT_DIR/../lib/ruled-notes.ts" GSTACK_REVIEW_WTREE="$WTREE" bun --no-env-file --config="${GSTACK_BUN_CONFIG:-/dev/null}" -e ' const { reviewFreshness, rulingGap, withCollectedFindings } = await import(process.env.GSTACK_REVIEW_LIB); const { dispositionedOn, gateKey, readGateDispositions, ruledOn, showDispositioned } = await import(process.env.GSTACK_GATE_LIB); const { readRuledNotes } = await import(process.env.GSTACK_RULED_LIB); let dispositions = []; try { dispositions = readGateDispositions(process.cwd()); } catch (e) { console.error("gstack-review-read: gate dispositions unreadable, none applied: " + e.message); } let rulings = []; try { rulings = readRuledNotes(process.cwd()); } catch (e) { console.error("gstack-review-read: rulings unreadable, none applied: " + e.message); } const recs = (await Bun.stdin.text()).split("\n").filter(Boolean).map(line => { try { const rec = JSON.parse(line); return rec && typeof rec === "object" && !Array.isArray(rec) ? rec : null; } catch { return null; } }); withCollectedFindings(recs.filter(Boolean)); const now = process.env.GSTACK_REVIEW_WTREE; let baseMerged; if (now && now !== "unknown" && recs.some(rec => rec && [rec.wtree, rec.review_binding?.end_wtree].some(w => typeof w === "string" && w !== now))) { try { const { baseMergeOnly } = await import(process.env.GSTACK_PATCH_LIB); const { resolveBaseBranch } = await import(process.env.GSTACK_POLICY_LIB); const merges = new Map(); let base; baseMerged = (rec, wtree) => { const key = rec.commit_full + " " + wtree; if (!merges.has(key)) merges.set(key, baseMergeOnly(process.cwd(), base ??= resolveBaseBranch(process.cwd()), rec.commit_full, wtree, now)); return merges.get(key); }; } catch (e) { console.error("gstack-review-read: base-merge check unavailable, rows read on other content stay STALE: " + e.message); } } for (const rec of recs) { try { if (!rec) throw new Error("malformed"); delete rec.review_freshness; delete rec.gate_dispositions; delete rec.ruled_notes; for (const f of Array.isArray(rec.findings) ? rec.findings : []) if (f && typeof f === "object") delete f.disposition; const closed = dispositionedOn(process.cwd(), rec, dispositions); const ruled = ruledOn(process.cwd(), rec, rulings); const ruledSet = new Set(ruled.map(n => n.finding)), gatedSet = new Set(closed.map(d => gateKey(d.finding, d.gate_id))); const freshness = reviewFreshness(rec, process.env.GSTACK_REVIEW_WTREE, ruledSet, gatedSet, baseMerged); const gap = freshness?.status === "UNVERIFIED" ? rulingGap(rec, ruledSet, gatedSet) : undefined; if (freshness) rec.review_freshness = gap ? { ...freshness, reason: freshness.reason + ": " + gap } : freshness; if (ruled.length) rec.ruled_notes = ruled.map(n => n.approval); showDispositioned(rec, closed); console.log(JSON.stringify(rec)); } catch { console.error("gstack-review-read: skipping malformed review row"); } } ' < "$GSTACK_HOME/projects/$SLUG/$BRANCH-reviews.jsonl" else echo "NO_REVIEWS" fi echo "---CONFIG---" "$SCRIPT_DIR/gstack-config" get skip_eng_review 2>/dev/null || echo "false" echo "---HEAD---" git rev-parse --short HEAD 2>/dev/null || echo "unknown" echo "---WTREE---" echo "$WTREE" echo "---TREE---" git rev-parse 'HEAD^{tree}' 2>/dev/null || echo "unknown" echo "---DIRTY---" if [ -n "$(git status --porcelain -uno 2>/dev/null | head -1)" ]; then echo "true"; else echo "false"; fi