Repository navigation
Expand file tree
/
Copy pathdocker_eval_guard.sh
More file actions
executable file
·245 lines (225 loc) · 11.2 KB
/
Copy pathdocker_eval_guard.sh
File metadata and controls
executable file
·245 lines (225 loc) · 11.2 KB
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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
#!/usr/bin/env bash
# docker_eval_guard.sh -- safe wrapper around the SWE-bench Docker eval for overnight audits.
#
# WHY: the AUTHORITATIVE SWE-bench Docker eval (swebench.harness.run_evaluation) can
# APPEAR hung for many minutes. CORRECTED DIAGNOSIS (2026-07-15 ~10:15 CDT, faulthandler
# all-threads dump of the parked eval; see ENV_KNOWLEDGE sec.25): it is NOT an orchestration
# deadlock. With the default namespace (swebench 4.x pulls PREBUILT images), build_container()
# calls docker images.pull() of a ~2.5GB eval image; the WORKER thread parks in socket.readinto
# streaming that pull while the MAIN thread waits in as_completed -- exactly the "~1% CPU, 0
# images, empty run log, futex_do_wait" signature earlier cycles mislabeled a deadlock. PROOF:
# an UNGUARDED run of pytest-dev__pytest-5227 COMPLETED in 259s (resolved=true). So these evals
# do finish; they just need patience for a cold prebuilt-image pull.
#
# WATCHDOG CAVEAT: the progress token below (image count | containers | run-log bytes) is
# INHERENTLY BLIND to an in-flight pull -- a partial image is not listed, and no container or
# run-log exists until the pull finishes -- so ALL THREE stay frozen during a legitimate pull.
# That is what falsely flagged working pulls as deadlocks. MITIGATION here: default --grace is
# widened to 900s so a cold pull is not aborted; the hard --timeout (1800s) remains the real
# backstop for a genuine hang. ROBUST REMEDY (recommended): pre-pull the prebuilt image
# (docker pull swebench/sweb.eval.x86_64.<id-with __->_1776_>:latest) BEFORE the eval, so
# run_evaluation's images.pull returns instantly and the token tracks the real test phase.
#
# This wrapper makes a hang FAIL FAST and VISIBLY:
# * validates the predictions file (exists, non-empty JSON list; each entry has instance_id + model_patch + model_name_or_path -- the last is required by run_evaluation and its absence KeyErrors only AFTER a multi-GB image pull)
# * preflights the docker image cache and warns when it is cold
# * EARLY-STALL WATCHDOG (new): samples a strictly-growing progress token
# (docker image count | running containers | bytes in the run logs); if it does not
# move for --grace seconds (default 300) it declares a deadlock and aborts in ~grace
# seconds instead of waiting out the full --timeout. A genuinely slow cold build streams
# into run_instance.log and grows images/build cache, so this does NOT false-positive on
# "slow but progressing".
# * enforces a hard timeout (SIGTERM then SIGKILL) as a backstop
# * on stall/timeout, force-removes any leftover containers for this run_id
# * distinguishes a stall/deadlock (exit 2) from a genuine eval error (exit 1)
# * --selftest verifies the watchdog decision logic WITHOUT docker (stalled child killed
# within grace; progressing child left alone)
#
# ANSWER-LEAKAGE: this only runs the authoritative scorer on the model's OWN prediction.
# It writes no results and injects nothing into any instance. General/scoring-layer only.
#
# Usage:
# docker_eval_guard.sh --preds PATH --run-id ID --instances "id1 id2 ..." \
# [--timeout SECS] [--grace SECS] [--venv PATH] [--dataset NAME] \
# [--workers N] [--dry-run]
# docker_eval_guard.sh --selftest
# Defaults: timeout=1800 grace=300 sample=20 venv=~/swebench-venv dataset=SWE-bench/SWE-bench_Lite workers=1
set -euo pipefail
PREDS="" RUN_ID="" INSTANCES="" TIMEOUT=1800
VENV="$HOME/swebench-venv" DATASET="SWE-bench/SWE-bench_Lite" WORKERS=1 DRY=0
GRACE="${GRACE:-900}" SAMPLE="${SAMPLE:-20}" SELFTEST=0
PREPULL="${PREPULL:-1}" PREPULL_TIMEOUT="${PREPULL_TIMEOUT:-1200}"
while [ $# -gt 0 ]; do
case "$1" in
--preds) PREDS="$2"; shift 2 ;;
--run-id) RUN_ID="$2"; shift 2 ;;
--instances) INSTANCES="$2"; shift 2 ;;
--timeout) TIMEOUT="$2"; shift 2 ;;
--grace) GRACE="$2"; shift 2 ;;
--sample) SAMPLE="$2"; shift 2 ;;
--venv) VENV="$2"; shift 2 ;;
--dataset) DATASET="$2"; shift 2 ;;
--workers) WORKERS="$2"; shift 2 ;;
--dry-run) DRY=1; shift ;;
--no-prepull) PREPULL=0; shift ;;
--prepull) PREPULL=1; shift ;;
--prepull-timeout) PREPULL_TIMEOUT="$2"; shift 2 ;;
--selftest) SELFTEST=1; shift ;;
*) echo "guard: unknown arg: $1" >&2; exit 3 ;;
esac
done
die() { echo "guard: $*" >&2; exit 3; }
# --- prebuilt image name for a SWE-bench instance id ---
# swebench 4.x pulls prebuilt eval images named swebench/sweb.eval.x86_64.<id>:latest
# with the instance id's "__" replaced by "_1776_" and lowercased (docker tags must be lowercase).
# e.g. pytest-dev__pytest-5227 -> swebench/sweb.eval.x86_64.pytest-dev_1776_pytest-5227:latest
_image_for_instance() {
local iid="$1" tag
tag=$(printf '%s' "$iid" | sed 's/__/_1776_/g' | tr '[:upper:]' '[:lower:]')
printf 'swebench/sweb.eval.x86_64.%s:latest' "$tag"
}
# --- progress probe: a token that GROWS while the eval RUNS TESTS ---
# CAVEAT (2026-07-15): these three are ALL frozen during a prebuilt-image PULL (partial image
# not listed; no container/run-log yet), so they only track the post-pull test phase. Keep
# --grace above the largest expected cold pull; the hard --timeout is the true hang backstop.
_progress_token() {
local imgs conts logb
imgs=$(docker images -q 2>/dev/null | wc -l | tr -d ' ')
conts=$(docker ps -q 2>/dev/null | wc -l | tr -d ' ')
logb=$(find "$HOME/swe/logs/run_evaluation/$RUN_ID" -type f -printf '%s\n' 2>/dev/null | awk '{s+=$1} END{print s+0}')
echo "${imgs}|${conts}|${logb}"
}
# --- watchdog: kill CHILD process group if PROBE output is unchanged for GRACE secs ---
# args: <child_pgid> <grace> <sample> <marker_path> <probe_fn_name>
# Relies on job control (set -m) so the child is its own process-group leader (pgid==pid).
_watchdog() {
local pgid="$1" grace="$2" sample="$3" marker="$4" probe="$5"
local last cur stalled=0
last="$($probe)"
while kill -0 "-$pgid" 2>/dev/null; do
sleep "$sample"
kill -0 "-$pgid" 2>/dev/null || break
cur="$($probe)"
if [ "$cur" = "$last" ]; then
stalled=$(( stalled + sample ))
if [ "$stalled" -ge "$grace" ]; then
echo "guard: STALL -- no progress for ${stalled}s (probe frozen at: $cur) -- killing eval" >&2
: > "$marker"
kill -TERM "-$pgid" 2>/dev/null || true
sleep 5
kill -KILL "-$pgid" 2>/dev/null || true
return 0
fi
else
stalled=0; last="$cur"
fi
done
return 0
}
# --- selftest: verify the watchdog DECISION LOGIC without docker ---
if [ "$SELFTEST" = "1" ]; then
set +e
set -m
tmp="$(mktemp -d)"
probe_file() { cat "$tmp/p" 2>/dev/null; }
# Case A: STALLED child (probe never changes) -> watchdog must kill within ~grace.
echo 0 > "$tmp/p"
sleep 60 & child=$!
t0=$(date +%s)
_watchdog "$child" 6 2 "$tmp/markerA" probe_file
wait "$child" 2>/dev/null
dt=$(( $(date +%s) - t0 ))
if [ ! -f "$tmp/markerA" ]; then echo "SELFTEST FAIL: stalled child was NOT flagged"; rm -rf "$tmp"; exit 1; fi
if [ "$dt" -gt 15 ]; then echo "SELFTEST FAIL: stall kill too slow (${dt}s > 15)"; rm -rf "$tmp"; exit 1; fi
echo "selftest A ok: stalled child killed in ${dt}s (<=15) and flagged"
# Case B: PROGRESSING child (probe grows each second) -> watchdog must NOT kill.
echo 0 > "$tmp/p"
( for i in $(seq 1 8); do echo "$i" > "$tmp/p"; sleep 1; done ) & writer=$!
sleep 8 & child=$!
_watchdog "$child" 4 1 "$tmp/markerB" probe_file
wait "$child" 2>/dev/null
kill "$writer" 2>/dev/null || true
if [ -f "$tmp/markerB" ]; then echo "SELFTEST FAIL: progressing child was wrongly killed"; rm -rf "$tmp"; exit 1; fi
echo "selftest B ok: progressing child NOT killed"
rm -rf "$tmp"
echo "SELFTEST PASS"
exit 0
fi
[ -n "$PREDS" ] || die "missing --preds"
[ -n "$RUN_ID" ] || die "missing --run-id"
[ -n "$INSTANCES" ] || die "missing --instances"
[ -f "$PREDS" ] || die "predictions file not found: $PREDS"
# predictions must be a non-empty JSON list
python3 - "$PREDS" <<'PY' || die "predictions file is not a non-empty JSON list"
import json,sys
d=json.load(open(sys.argv[1]))
assert isinstance(d,list) and len(d)>0, "not a non-empty list"
for e in d:
assert e.get("instance_id") and e.get("model_patch") and e.get("model_name_or_path"), "entry missing instance_id/model_patch/model_name_or_path (run_evaluation reads prediction[KEY_MODEL]=model_name_or_path and KeyErrors AFTER the image pull if absent)"
PY
[ -x "$VENV/bin/python" ] || die "venv python not found: $VENV/bin/python"
IMG_COUNT=$(docker images -q 2>/dev/null | wc -l | tr -d ' ')
if [ "$IMG_COUNT" = "0" ]; then
echo "guard: COLD docker cache (0 images) -- expect a long cold rebuild; timeout=${TIMEOUT}s grace=${GRACE}s"
else
echo "guard: docker cache has ${IMG_COUNT} images"
fi
# --- PRE-PULL the prebuilt image(s) BEFORE the eval (ROBUST unblock, 2026-07-15) ---
# run_evaluation streams a ~2-4GB prebuilt-image pull inside build_container(); during that
# pull the watchdog progress token (images|containers|run-log bytes) is frozen, which earlier
# cycles mislabeled a deadlock. Pulling here first makes run_evaluation's images.pull instant
# so the watchdog only ever measures the real test phase. Best-effort: a failed pre-pull just
# falls back to run_evaluation doing the pull (grace covers it). Skipped on --dry-run.
if [ "$PREPULL" = "1" ] && [ "$DRY" = "0" ]; then
for iid in $INSTANCES; do
img="$(_image_for_instance "$iid")"
if docker image inspect "$img" >/dev/null 2>&1; then
echo "guard: prepull -- already present: $img"
else
echo "guard: prepull -- docker pull $img (timeout ${PREPULL_TIMEOUT}s)"
if timeout "$PREPULL_TIMEOUT" docker pull "$img" >/dev/null 2>&1; then
echo "guard: prepull ok -- $img"
else
echo "guard: prepull WARN -- could not pre-pull $img; run_evaluation will pull it (watchdog grace covers a cold pull)" >&2
fi
fi
done
fi
CMD=( timeout --signal=TERM --kill-after=30 "$TIMEOUT"
"$VENV/bin/python" -m swebench.harness.run_evaluation
--dataset_name "$DATASET"
--predictions_path "$PREDS"
--run_id "$RUN_ID"
--instance_ids $INSTANCES
--max_workers "$WORKERS" )
echo "guard: command:"
printf ' %q' "${CMD[@]}"; echo
if [ "$DRY" = "1" ]; then
echo "guard: [dry-run] not executing."
exit 0
fi
set +e
set -m # job control: the backgrounded eval gets its own process group
"${CMD[@]}" &
EVAL_PID=$! # == process-group leader (pgid) under set -m
MARKER="$(mktemp -u)"
echo "guard: early-stall watchdog armed (grace=${GRACE}s sample=${SAMPLE}s; hard timeout=${TIMEOUT}s backstop)"
_watchdog "$EVAL_PID" "$GRACE" "$SAMPLE" "$MARKER" _progress_token &
WD_PID=$!
wait "$EVAL_PID"; rc=$?
kill "$WD_PID" 2>/dev/null || true
wait "$WD_PID" 2>/dev/null
set +m
set -e
if [ -f "$MARKER" ]; then
rm -f "$MARKER"
echo "guard: EARLY DEADLOCK-STALL abort (no progress for >=${GRACE}s) -- cleaning up run_id containers"
docker ps -a --format '{{.Names}}' 2>/dev/null | grep -F ".$RUN_ID" | xargs -r docker rm -f 2>/dev/null || true
exit 2
fi
if [ $rc -eq 124 ] || [ $rc -eq 137 ]; then
echo "guard: TIMEOUT/DEADLOCK after ${TIMEOUT}s (rc=$rc) -- cleaning up run_id containers"
docker ps -a --format '{{.Names}}' 2>/dev/null | grep -F ".$RUN_ID" | xargs -r docker rm -f 2>/dev/null || true
exit 2
fi
exit $rc