#!/usr/bin/env bash
# Greenlight verifier: replays a run on your machine with the same harness and the same image recipe.
#   curl -fsSL https://www.greenlightlabs.xyz/verifier/greenlight-verify.sh | bash -s -- <requestId> <file.patch> [hidden-test-file]
# Needs Docker. The hidden test is known only to the request's author: without it, the replay runs everything else.
set -euo pipefail
SITE="${GREENLIGHT_SITE:-https://www.greenlightlabs.xyz}"
id="${1:?request id}"; patch="${2:?patch file}"; hidden="${3:-}"
command -v docker >/dev/null || { echo "docker is required"; exit 1; }
work="$(mktemp -d)"
trap 'rm -rf "$work"' EXIT
curl -fsSL "$SITE/verifier/Dockerfile" -o "$work/Dockerfile"
curl -fsSL "$SITE/verifier/runner.mjs" -o "$work/runner.mjs"
curl -fsSL "$SITE/api/v1/requests/$id" -o "$work/request.json"
cp "$patch" "$work/patch.diff"
[ -n "$hidden" ] && cp "$hidden" "$work/hidden"
echo "building the runner image (Debian bookworm, Node 22, Python 3.12)..."
docker build -q -t greenlight-runner:local "$work" >/dev/null
docker run --rm -v "$work:/in" greenlight-runner:local node -e '
const fs = require("fs");
const r = JSON.parse(fs.readFileSync("/in/request.json", "utf8"));
if (!r.spec) { console.error("this request has no spec on the site"); process.exit(1); }
const hidden = fs.existsSync("/in/hidden") ? fs.readFileSync("/in/hidden", "utf8") : null;
const spec = { ...r.spec };
if (spec.hidden && !hidden) { console.error("note: replaying without the hidden test (only the request author has it)"); spec.hidden = null; }
fs.writeFileSync("/in/job.json", JSON.stringify({ kind: "patch", spec, failingTest: { content: r.failingTest ? r.failingTest.content : null }, hidden: hidden && spec.hidden ? { content: hidden } : null, patch: fs.readFileSync("/in/patch.diff", "utf8"), baseline: r.spec.baseline }));
'
docker run --rm --cap-add SYS_ADMIN --security-opt seccomp=unconfined -e GL_LOCAL_JOB=/in/job.json -v "$work:/in" greenlight-runner:local
