#!/bin/sh
# Pre-push fitness guard
# Runs nexus-agents fitness-audit before push to prevent score degradation.
# Consensus-approved enhancement (3/3 votes, simple_majority strategy).
# Skip with: git push --no-verify

# Only run if the CLI is built (skip gracefully otherwise)
CLI_PATH="packages/nexus-agents/dist/cli.js"
if [ ! -f "$CLI_PATH" ]; then
  echo "[fitness-guard] CLI not built, skipping fitness check"
  exit 0
fi

echo "[fitness-guard] Running fitness audit..."
# Silence info-level logger output so stdout is pure JSON (#1900).
# Without NEXUS_LOG_LEVEL=error, the "Fitness audit complete" info log
# precedes the JSON payload and grep picks the wrong "score" value.
RESULT=$(NEXUS_LOG_LEVEL=error node "$CLI_PATH" fitness-audit --format=json 2>/dev/null)

if [ $? -ne 0 ]; then
  echo "[fitness-guard] Fitness audit failed to run, skipping"
  exit 0
fi

SCORE=$(echo "$RESULT" | grep -o '"score": *[0-9]*' | head -1 | grep -o '[0-9]*')

if [ -z "$SCORE" ]; then
  echo "[fitness-guard] Could not parse fitness score, skipping"
  exit 0
fi

THRESHOLD=90

if [ "$SCORE" -lt "$THRESHOLD" ]; then
  echo ""
  echo "[fitness-guard] BLOCKED: Fitness score $SCORE/100 < $THRESHOLD threshold"
  echo "[fitness-guard] Run 'nexus-agents fitness-audit' for details"
  echo "[fitness-guard] Skip with: git push --no-verify"
  exit 1
fi

echo "[fitness-guard] Fitness score: $SCORE/100 (threshold: $THRESHOLD)"

# Model-string drift check (#2199 Child 4) — blocking
# Rollback: set NEXUS_DRIFT_ADVISORY=1 in your shell or skip with --no-verify.
echo "[drift-guard] Running model-string drift check..."
if NEXUS_DRIFT_ADVISORY=0 npx --quiet tsx scripts/check-model-string-drift.ts; then
  echo "[drift-guard] No new model-version drift detected."
else
  echo ""
  echo "[drift-guard] BLOCKED: hardcoded model-version string outside config/."
  echo "[drift-guard] Migrate to config/model-capabilities.ts or add an"
  echo "[drift-guard] allowlist entry referencing companion epic #2200."
  echo "[drift-guard] Skip with: git push --no-verify"
  exit 1
fi
