File size: 4,777 Bytes
4514571
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
#!/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 <<EOF
{"reward": $reward, "patch_applied": $patch_applied, "fail_to_pass": $fail_to_pass, "pass_to_pass": $pass_to_pass, "deterministic": $deterministic, "setup_completed": $setup_completed, "fail_to_pass_exit_code": $fail_to_pass_exit_code, "fail_to_pass_repeat_exit_code": $fail_to_pass_repeat_exit_code, "pass_to_pass_exit_code": $pass_to_pass_exit_code}
EOF
exit 0