#!/bin/bash set -uo pipefail mkdir -p /logs/verifier patch_applied=1 fail_to_pass=0 pass_to_pass=0 deterministic=0 setup_completed=0 fail_to_pass_exit_code=-1 fail_to_pass_repeat_exit_code=-1 pass_to_pass_exit_code=-1 verifier_cache="" kill_verifier_processes() { pkill -KILL -u "$(id -u verifier)" 2>/dev/null || true; } # Some toolchains fetch dependencies at test runtime (e.g. Next.js e2e installs), # so a single registry connection reset must not be misread as a dead test. Retry # only infrastructure-style failures with backoff; real assertion failures fail fast. run_verifier_command() { local logfile logfile="$(mktemp /tmp/selfbench-verifier-command-XXXXXX.log)" local attempt=1 local status=1 while [ "$attempt" -le 3 ]; do : > "$logfile" runuser -u verifier -- env PATH="/usr/local/go/bin:/usr/local/cargo/bin:/usr/local/bin:/usr/local/sbin:/usr/bin:/usr/sbin:/bin:/sbin" UV_CACHE_DIR="$verifier_cache" UV_NO_BUILD_ISOLATION=1 bash -lc "$1" >"$logfile" 2>&1 status=$? if [ "$status" -eq 0 ]; then break fi if [ "$attempt" -lt 3 ] && grep -qE 'ECONNRESET|ETIMEDOUT|ESOCKETTIMEDOUT|ENOTFOUND|EAI_AGAIN|META_FETCH_FAIL|FetchError|EPIPE|EPERM|registry\.npmjs' "$logfile"; then sleep "$((10 * attempt))" attempt=$((attempt + 1)) continue fi break done cat "$logfile" rm -f "$logfile" kill_verifier_processes return "$status" } protect_held_out_path() { local path="$1" chown -R root:root -- "$path" chmod -R a-w,go+rX -- "$path" } if [ ! -f /opt/selfbench/agent.patch ]; then patch_applied=0 elif [ -s /opt/selfbench/agent.patch ]; then git -C /app apply --binary --whitespace=nowarn --exclude='test/e2e/app-dir/actions-unrecognized/actions-unrecognized.test.ts' --exclude='test/e2e/app-dir/actions-unrecognized/actions-unrecognized.test.ts/*' --exclude='test/e2e/app-dir/no-server-actions/no-server-actions.test.ts' --exclude='test/e2e/app-dir/no-server-actions/no-server-actions.test.ts/*' /opt/selfbench/agent.patch || patch_applied=0 fi if [ "$patch_applied" -eq 1 ]; then setup_completed=1; fi if [ "$patch_applied" -eq 1 ] && [ "$setup_completed" -eq 1 ]; then kill_verifier_processes git -C /app restore --source=HEAD --staged --worktree -- 'test/e2e/app-dir/actions-unrecognized/actions-unrecognized.test.ts' 'test/e2e/app-dir/no-server-actions/no-server-actions.test.ts' 2>/dev/null || true git -C /app clean -fd -- 'test/e2e/app-dir/actions-unrecognized/actions-unrecognized.test.ts' 'test/e2e/app-dir/no-server-actions/no-server-actions.test.ts' >/dev/null 2>&1 || true git -C /app apply --binary --whitespace=nowarn /tests/test.patch || patch_applied=0 if [ "$patch_applied" -eq 1 ]; then for protected_path in '/app/test/e2e/app-dir/actions-unrecognized/actions-unrecognized.test.ts' '/app/test/e2e/app-dir/no-server-actions/no-server-actions.test.ts'; do protect_held_out_path "$protected_path"; done fi rm -f /tests/test.patch fi if [ "$patch_applied" -eq 1 ] && [ "$setup_completed" -eq 1 ]; then cd '/app/.' verifier_cache="$(mktemp -d /tmp/selfbench-verifier-uv-XXXXXX)" cp -a /opt/uv-cache/. "$verifier_cache"/ chown -R verifier:verifier "$verifier_cache" if run_verifier_command 'pnpm --filter next build >/dev/null && pnpm test-dev '"'"'test/e2e/app-dir/actions-unrecognized/actions-unrecognized.test.ts'"'"' '"'"'test/e2e/app-dir/no-server-actions/no-server-actions.test.ts'"'"''; then fail_to_pass_exit_code=0 fail_to_pass=1 if run_verifier_command 'pnpm --filter next build >/dev/null && pnpm test-dev '"'"'test/e2e/app-dir/actions-unrecognized/actions-unrecognized.test.ts'"'"' '"'"'test/e2e/app-dir/no-server-actions/no-server-actions.test.ts'"'"''; then fail_to_pass_repeat_exit_code=0 deterministic=1 else fail_to_pass_repeat_exit_code=$? fi else fail_to_pass_exit_code=$? fi if run_verifier_command 'pnpm --filter next build >/dev/null && pnpm test-dev '"'"'test/e2e/app-dir/prerender-encoding/prerender-encoding.test.ts'"'"' '"'"'test/e2e/app-dir/metadata-svg-icon/metadata-svg-icon.test.ts'"'"''; then pass_to_pass_exit_code=0 pass_to_pass=1 else pass_to_pass_exit_code=$? fi rm -rf "$verifier_cache" fi reward=0 if [ "$patch_applied" -eq 1 ] && [ "$fail_to_pass" -eq 1 ] && [ "$pass_to_pass" -eq 1 ] && [ "$deterministic" -eq 1 ]; then reward=1; fi cat > /logs/verifier/reward.json <