worldbox-observer-mod/scripts/harness-run.sh
2026-07-14 14:37:34 -05:00

90 lines
2.4 KiB
Bash
Executable file

#!/usr/bin/env bash
# Run an IdleSpectator agent harness scenario (or raw JSONL commands).
# Usage:
# ./scripts/harness-run.sh smoke
# ./scripts/harness-run.sh tip_match
# ./scripts/harness-run.sh --cmd '{"id":"1","action":"snapshot"}'
set -euo pipefail
ROOT="$(cd "$(dirname "$0")/.." && pwd)"
MOD="${IDLE_SPECTATOR_MOD:-$ROOT/IdleSpectator}"
HARNESS="$MOD/.harness"
LOG="${WORLDBOX_LOG:-$HOME/.config/unity3d/mkarpenko/WorldBox/Player.log}"
TIMEOUT_SEC="${HARNESS_TIMEOUT:-90}"
mkdir -p "$HARNESS"
scenario=""
raw_cmd=""
while [[ $# -gt 0 ]]; do
case "$1" in
--cmd)
raw_cmd="$2"
shift 2
;;
--timeout)
TIMEOUT_SEC="$2"
shift 2
;;
-*)
echo "Unknown flag: $1" >&2
exit 2
;;
*)
scenario="$1"
shift
;;
esac
done
if [[ -z "$scenario" && -z "$raw_cmd" ]]; then
scenario="smoke"
fi
rm -f "$HARNESS/last-result.json"
: > "$HARNESS/results.jsonl"
# While this file exists, harness auto-closes Welcome Back / blocking UI.
touch "$HARNESS/auto-dismiss"
{
echo '{"id":"batch","action":"reset_counters"}'
echo '{"id":"dismiss","action":"dismiss_windows"}'
if [[ -n "$raw_cmd" ]]; then
echo "$raw_cmd"
else
echo "{\"id\":\"run\",\"action\":\"scenario\",\"value\":\"$scenario\"}"
fi
} > "$HARNESS/commands.jsonl"
echo "Harness queued: ${scenario:-raw} (mod=$MOD)"
echo "Waiting up to ${TIMEOUT_SEC}s for last-result.json ..."
deadline=$((SECONDS + TIMEOUT_SEC))
while (( SECONDS < deadline )); do
if [[ -f "$HARNESS/last-result.json" ]]; then
# NOTE: do not name this `status` - zsh treats that as read-only.
result_status="$(python3 -c "import json;print(json.load(open('$HARNESS/last-result.json')).get('status',''))" 2>/dev/null || true)"
if [[ "$result_status" == "PASS" || "$result_status" == "FAIL" || "$result_status" == "error" ]]; then
echo "===== last-result.json ====="
cat "$HARNESS/last-result.json"
echo "===== recent harness log ====="
if [[ -f "$LOG" ]]; then
rg -n "\[IdleSpectator\]\[(HARNESS|ASSERT|SNAP)\]|dismissed window" "$LOG" | tail -n 40 || true
fi
if [[ "$result_status" == "PASS" ]]; then
exit 0
fi
exit 1
fi
fi
sleep 0.5
done
echo "Timed out after ${TIMEOUT_SEC}s" >&2
if [[ -f "$HARNESS/last-result.json" ]]; then
cat "$HARNESS/last-result.json" >&2 || true
fi
if [[ -f "$LOG" ]]; then
rg -n "IdleSpectator|error CS|Exception" "$LOG" | tail -n 50 >&2 || true
fi
exit 1