#!/usr/bin/env bash
# Goodhart guard: reject candidates that lower quality while improving timing
set -uo pipefail

HERE="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
TARGET="${ATLAS_OPTIMIZE_TARGET:-$2}"
REF_SCORE="${ATLAS_REF_SCORE:-$1}"

NEW_OUT="$(mktemp)"
NEW_START=$(date +%s.%N)
( python3 "$TARGET" ) >"$NEW_OUT" 2>&1
NEW_RC=$?
NEW_END=$(date +%s.%N)
NEW_ELAPSED=$(awk "BEGIN { print $NEW_END - $NEW_START }")

if [ "$NEW_RC" -ne 0 ]; then
  echo "[gate] candidate failed — rejecting"
  exit 1
fi

NEW_SCORE=$(python3 "$HERE/score.py" --stdout "$NEW_OUT" --elapsed "$NEW_ELAPSED" 2>/dev/null | python3 -c "import json,sys; print(json.load(sys.stdin)['score'])")

# Accept if score >= reference (higher is better)
ACCEPT=$(python3 -c "print('yes' if $NEW_SCORE >= $REF_SCORE - 0.01 else 'no')")
if [ "$ACCEPT" = "yes" ]; then
  echo "[gate] accepted (score=$NEW_SCORE >= ref=$REF_SCORE)"
  exit 0
else
  echo "[gate] rejected (score=$NEW_SCORE < ref=$REF_SCORE)"
  exit 1
fi
