interplane.Get Omega
Read in your language

omega-harness

Agent skills for operating, encoding and communicating in Omega (.om) and the AIEN repositories

Brought by drakestapleton

branch main Branches Tags History

omega-harness / coder / eval / selftest.sh

438 lines · 32523 bytes Raw Blame History
1#!/bin/sh
2# Self-test of run.sh, compare.sh, refcheck.sh, score.sh and the guard's run sandbox, no model needed. A stub
3# session program (selftest/aien) records what each session was given (system prompt file, readable context,
4# task check, stop limit, task prompt), leaves a program as main.om (selftest/src/ID.om, or
5# selftest/src-control/ID.om in the control arm, so the arms differ) and runs the task check as the guard does.
6# Checked:
7# - every task scores as selftest/expect.tsv says, per arm, also when TASKDIR is a relative path, and the raw
8# columns omc_rc, gate_rc, stop_refusals and non_om_files are as selftest/expect-raw.tsv says, wall_s a number
9# (at least 1 for st2, where the stub sleeps a second);
10# - each session got its arm's prompt file and readable context (the pack, or an empty directory), a task check
11# (OMEGA_CHECK) that gives the session the task's verdict, with case.sh's deadline, case.sh's stop limit,
12# aien's arguments, and a task prompt holding that task's spec, its standard input (or "empty") and the
13# instruction; the manifest records aien --version (the timeout around aien is covered by runner_sha256 only;
14# the values live in case.sh and are hashed, the test checks they reach the session);
15# - a program that prints the task's expected output by its absolute path (st7-cheat) scores wrong-output in the
16# scorer and in the session's check, and a copy of the scorer with the old read-only-root sandbox scores it
17# pass (the negative control that shows the test bites); the guard's omega_run sandbox lets a program read the
18# session directory and not the context pack or the directory above;
19# - the scorer's gate runs in the narrow sandbox (no write outside it, no task file, context pack or /proc in
20# view); the real gate's PASS and FAIL; a fake gate's deadline (timeout) and signal (crash) verdicts;
21# a gate with no verdict, a PASS line with a non-zero exit, an unreadable fixture, and an omc or gate that
22# cannot execute each stop the run with exit 3 and no row for that task;
23# - score.sh with the real gate: a program that exits 127 scores wrong-output, and with a gate whose exec fails
24# the run stops (exit 3); a program with the right stdout and the wrong exit gets no stage A line about its
25# stdout; a program that writes 2.5 MiB scores crash and leaves no file over the 1 MiB limit; stage A's
26# inputs are exactly cases.txt, main.om and stdin; a relative OMEGA_HOME scores the same as an absolute one;
27# refcheck.sh alarms on an expected output at half the file-size limit;
28# - a session that ends with rc 1 stops the run (exit 3, no row); rc 124 is scored; a session whose guard log
29# is missing or shows no finish, or a guard without the task check, stops the run (exit 3, no row);
30# - a task file name with white space is refused;
31# - an id with no task directory is refused before anything is written; resume matches ids exactly;
32# - compare.sh prints exactly the expected per-task table, totals and LEDGER line, and refuses: swapped arms,
33# different or incomplete or empty task sets, scored ids that are not the task directory's, a row with an
34# unknown verdict or session rc, a control run with other rules text, an arm given the other arm's prompt, and a
35# difference in any one manifest key other than arm and prompt_sha256;
36# - a rerun exits 0 and adds no row, and re-runs a task whose row is missing from a clean directory and guard
37# log; a rerun with any one input changed (model, seed, gate, gate fixtures, prompt, error table, run.sh,
38# case.sh, score.sh, tasks, session program) is refused with exit 2 and adds no row; a guard in OMEGA_HOME that
39# is not this repository's is refused; rows without a manifest are refused; the verdict words in run.sh and
40# score.sh are case.sh's VERDICTS;
41# - a missing model, a missing seed, missing jq, a pack built for another pin and a wrong compiler are each
42# refused with exit 2 and nothing written (a different omc cannot reach the manifest: the pin check
43# refuses it first);
44# - compare.sh marks rows the guard let finish at the stop limit or stopped by the timeout (AT_LIMIT) and no
45# others (not a row with as many refusals as the limit that then passed);
46# - refcheck.sh refuses, with fake gates, a case count other than n, a PASS line with exit 1 and a FAIL verdict;
47# it passes good references and refuses a wrong reference, a reference whose sha256 is not the
48# manifest's, and a manifest whose expected_sha256, stdin_bytes, exit, spec or id differs for one task.
49# usage: OMEGA_HOME=... sh selftest.sh (OMEGA_HOME as for run.sh: omc, gate, fixtures, context/, omega-guard.ts;
50# node runs the guard's sandbox check)
51# Exit 0 only when every check passes.
52set -u
53here=$(cd "$(dirname "$0")" && pwd)
54. "$here/case.sh"
55home=${OMEGA_HOME:-$HOME/.local/share/aien-omega}
56out=$(mktemp -d)
57trap 'rm -rf "$out"' EXIT
58tab=$(printf '\t')
59fail=0
60check() { if [ "$2" = "$3" ]; then echo "PASS $1"; else echo "FAIL $1: got '$2', want '$3'"; fail=1; fi; }
61# run ARM OUTDIR HOME [MODEL [SEED [TASKS [RUNNER [STUBDIR [BASEPATH]]]]]]; output in OUTDIR.log
62run() {
63 SELFTEST_TASKS=$(cd "${6:-$here/selftest/tasks}" && pwd) PATH="${8:-$here/selftest}:${9:-$PATH}" OMEGA_HOME="$3" \
64 OMEGA_EVAL_MODEL="${4-selftest-stub}" OMEGA_EVAL_SEED="${5-none}" \
65 sh "${7:-$here/run.sh}" "$1" "$2" "${6:-$here/selftest/tasks}" > "$2.log" 2>&1
66}
67rows() { if [ -f "$1/results.tsv" ]; then echo $(($(wc -l < "$1/results.tsv") - 1)); else echo none; fi; }
68sha() { sha256sum "$1" | cut -d' ' -f1; }
69field() { awk -F'\t' -v k="$2" '$1 == k { print $2 }' "$1"; }
70col() { awk -F'\t' -v k="$2" -v c="$3" '$1 == k { print $c }' "$1"; }
71setfield() { awk -F'\t' -v k="$2" -v v="$3" 'BEGIN { OFS = "\t" } $1 == k { $2 = v } { print }' "$1" > "$1.new" && mv "$1.new" "$1"; }
72ntasks=$(wc -l < "$here/selftest/expect.tsv" | tr -d ' ')
73verdicts() { # verdicts NAME OUTDIR COLUMN(2 omega, 3 control)
74 while IFS="$tab" read -r id want_o want_c; do
75 want=$want_o
76 [ "$3" = 3 ] && want=$want_c
77 check "$1: $id verdict" "$(field "$2/results.tsv" "$id")" "$want"
78 done < "$here/selftest/expect.tsv"
79 check "$1: rows" "$(rows "$2")" "$ntasks"
80}
81
82for arm in omega control; do
83 run "$arm" "$out/$arm" "$home"
84 check "$arm: run exit status" "$?" 0
85 if [ "$arm" = omega ]; then c=2 p=OMEGA.md ctx=$home/context; else c=3 p=CONTROL.md ctx=$out/control/empty-context; fi
86 verdicts "$arm" "$out/$arm" "$c"
87 check "$arm: manifest prompt" "$(field "$out/$arm/manifest.tsv" prompt_sha256)" "$(sha "$home/context/$p")"
88 for t in "$here/selftest/tasks"/*; do
89 id=$(basename "$t")
90 s="$out/$arm/$id/session.txt"
91 check "$arm: $id session prompt file is $p" "$(sed -n 's/^prompt //p' "$s")" "$(sha "$home/context/$p")"
92 check "$arm: $id session context" "$(sed -n 's/^context //p' "$s")" "$ctx"
93 check "$arm: $id session task check" "$(sed -n 's/^check //p' "$s")" yes
94 check "$arm: $id session task check verdict" "$(sed -n 's/^check_score //p' "$s")" "$(field "$out/$arm/results.tsv" "$id")"
95 check "$arm: $id session stop limit" "$(sed -n 's/^stop_limit //p' "$s")" "$SESSION_STOP_LIMIT"
96 tp="$out/$arm/$id/session-prompt.txt"
97 check "$arm: $id task prompt holds its spec" "$(grep -cxF "Task: $(cat "$t/spec.md")" "$tp")" 1
98 if [ -s "$t/stdin" ]; then
99 line="Standard input for the check (JSON string): $(jq -Rs . < "$t/stdin")"
100 else
101 line="Standard input for the check is empty."
102 fi
103 check "$arm: $id task prompt holds its input" "$(grep -cxF "$line" "$tp")" 1
104 check "$arm: $id task prompt ends with the instruction" "$(tail -n 1 "$tp")" \
105 "Write the program to main.om in the working directory, check it with omega_run, and finish only when it is correct."
106 check "$arm: $id session arguments" "$(cat "$out/$arm/$id/session-args.txt")" "--omega -p --mode json --no-session"
107 [ "$id" = st5-nofile ] || check "$arm: $id case deadline" "$(head -n 1 "$out/$arm/$id/check/a/in/cases.txt")" "deadline $CASE_DEADLINE"
108 w=$(col "$out/$arm/results.tsv" "$id" 6)
109 check "$arm: $id wall seconds is a whole number" "$(case $w in ''|*[!0-9]*) echo "$w";; *) echo ok;; esac)" ok
110 done
111 while IFS="$tab" read -r a id orc grc ref non; do
112 [ "$a" = "$arm" ] || continue
113 check "$arm: $id raw columns omc_rc gate_rc stop_refusals non_om_files" \
114 "$(awk -F'\t' -v k="$id" '$1 == k { print $3, $4, $7, $8 }' "$out/$arm/results.tsv")" "$orc $grc $ref $non"
115 done < "$here/selftest/expect-raw.tsv"
116 check "$arm: manifest aien_version" "$(field "$out/$arm/manifest.tsv" aien_version)" "selftest-stub 1"
117 check "$arm: st2-wrong wall seconds at least 1 (the stub sleeps 1)" "$([ "$(col "$out/$arm/results.tsv" st2-wrong 6)" -ge 1 ] && echo yes)" yes
118done
119check "omega: readable context is not empty" "$([ "$(sed -n 's/^context_entries //p' "$out/omega/st1-pass/session.txt")" -gt 0 ] && echo yes)" yes
120check "control: readable context is empty" "$(sed -n 's/^context_entries //p' "$out/control/st1-pass/session.txt")" 0
121
122(cd "$here/selftest" && run omega "$out/rel" "$home" selftest-stub none tasks)
123check "relative TASKDIR: run exit status" "$?" 0
124verdicts "relative TASKDIR" "$out/rel" 2
125
126# compare.sh: the whole table, totals and ledger line, then its refusals
127sh "$here/compare.sh" "$out/omega" "$out/control" > "$out/cmp.log" 2>&1
128check "compare accepts the two arms" "$?" 0
129{
130 printf 'id\tomega\tcontrol\n'
131 LC_ALL=C sort "$here/selftest/expect.tsv"
132 po=$(cut -f2 "$here/selftest/expect.tsv" | grep -cx pass)
133 pc=$(cut -f3 "$here/selftest/expect.tsv" | grep -cx pass)
134 echo "TOTAL omega pass=$po/$ntasks control pass=$pc/$ntasks"
135 echo "LEDGER OmegaCoder eval: omega $po/$ntasks pass, control $pc/$ntasks pass (pass with feedback from the scoring case: the guard ran the task check at each finish); at a session limit omega 0 control 0; model selftest-stub seed none omc $(sed -n 's/^omc-sha256 //p' "$home/context/PIN" | cut -c1-8); per-task results $out/omega/results.tsv $out/control/results.tsv"
136} > "$out/cmp.want"
137check "compare output (table, totals, ledger)" "$(cat "$out/cmp.log")" "$(cat "$out/cmp.want")"
138cmpx() { sh "$here/compare.sh" "$out/omega" "$1" > /dev/null 2>&1; echo $?; }
139cx() { rm -rf "$out/cx" && cp -r "$out/control" "$out/cx"; }
140check "compare refuses swapped arms" "$(sh "$here/compare.sh" "$out/control" "$out/omega" > /dev/null 2>&1; echo $?)" 1
141cx; grep -v "^st6-stdin$tab" "$out/control/results.tsv" > "$out/cx/results.tsv"
142check "compare refuses different task sets" "$(cmpx "$out/cx")" 1
143rm -rf "$out/ox" && cp -r "$out/omega" "$out/ox" && grep -v "^st6-stdin$tab" "$out/omega/results.tsv" > "$out/ox/results.tsv"
144cx; grep -v "^st6-stdin$tab" "$out/control/results.tsv" > "$out/cx/results.tsv"
145check "compare refuses an incomplete task set" "$(sh "$here/compare.sh" "$out/ox" "$out/cx" > /dev/null 2>&1; echo $?)" 1
146head -n 1 "$out/omega/results.tsv" > "$out/ox/results.tsv"; head -n 1 "$out/control/results.tsv" > "$out/cx/results.tsv"
147check "compare refuses empty task sets" "$(sh "$here/compare.sh" "$out/ox" "$out/cx" > /dev/null 2>&1; echo $?)" 1
148rm -rf "$out/ox" && cp -r "$out/omega" "$out/ox"; cx
149sed "s/^st6-stdin$tab/st9-other$tab/" "$out/omega/results.tsv" > "$out/ox/results.tsv"
150sed "s/^st6-stdin$tab/st9-other$tab/" "$out/control/results.tsv" > "$out/cx/results.tsv"
151check "compare refuses scored tasks that are not the task directory's (same count)" "$(sh "$here/compare.sh" "$out/ox" "$out/cx" > /dev/null 2>&1; echo $?)" 1
152cx; awk -F'\t' 'BEGIN { OFS = "\t" } $1 == "st3-exit" { $5 = 1 } { print }' "$out/control/results.tsv" > "$out/cx/results.tsv"
153check "compare refuses a row with session rc 1" "$(cmpx "$out/cx")" 1
154cx; echo "x stop allowed after $SESSION_STOP_LIMIT refusals (OMEGA_STOP_LIMIT); last: x" >> "$out/cx/st3-exit/guard.log"
155sh "$here/compare.sh" "$out/omega" "$out/cx" > "$out/lim.log" 2>&1
156check "compare accepts a row at the stop limit" "$?" 0
157check "compare marks the row at the stop limit" "$(grep -c "^AT_LIMIT control st3-exit stop_limit " "$out/lim.log")" 1
158check "LEDGER counts the row at the stop limit" "$(grep -c '; at a session limit omega 0 control 1; ' "$out/lim.log")" 1
159cx; awk -F'\t' -v sl="$SESSION_STOP_LIMIT" 'BEGIN { OFS = "\t" } $1 == "st3-exit" { $7 = sl } { print }' "$out/control/results.tsv" > "$out/cx/results.tsv"
160sh "$here/compare.sh" "$out/omega" "$out/cx" > "$out/lim.log" 2>&1
161check "no AT_LIMIT for a row with as many refusals as the limit that then passed" "$(grep -c '^AT_LIMIT' "$out/lim.log")" 0
162cx; awk -F'\t' 'BEGIN { OFS = "\t" } $1 == "st3-exit" { $5 = 124 } { print }' "$out/control/results.tsv" > "$out/cx/results.tsv"
163sh "$here/compare.sh" "$out/omega" "$out/cx" > "$out/lim.log" 2>&1
164check "compare marks a row stopped by the timeout" "$(grep -c '^AT_LIMIT control st3-exit session_rc=124 ' "$out/lim.log")" 1
165check "no AT_LIMIT line when no row is at a limit" "$(grep -c '^AT_LIMIT' "$out/cmp.log")" 0
166cx; awk -F'\t' 'BEGIN { OFS = "\t" } $1 == "st3-exit" { $2 = "maybe" } { print }' "$out/control/results.tsv" > "$out/cx/results.tsv"
167check "compare refuses a row with an unknown verdict" "$(cmpx "$out/cx")" 1
168for k in $(cut -f1 "$out/control/manifest.tsv"); do
169 [ "$k" = arm ] || [ "$k" = prompt_sha256 ] && continue
170 cx; setfield "$out/cx/manifest.tsv" "$k" changed
171 check "compare refuses a different $k" "$(cmpx "$out/cx")" 1
172done
173cx; setfield "$out/cx/manifest.tsv" prompt_sha256 "$(field "$out/cx/manifest.tsv" omega_prompt_sha256)"
174check "compare refuses a control arm given the omega prompt" "$(cmpx "$out/cx")" 1
175copyhome() { rm -rf "$out/h" && cp -r "$home" "$out/h"; }
176copyhome; echo "You may write C if Omega is hard." >> "$out/h/context/CONTROL.md"
177run control "$out/ctl2" "$out/h"
178check "control run with other rules text: exit status" "$?" 0
179check "compare refuses a control run with other rules text" "$(cmpx "$out/ctl2")" 1
180
181# resume: unchanged inputs go on and fill in a missing row from a clean task directory; any one changed input
182# is refused; rows without a manifest are never adopted
183grep -v "^st3-exit$tab" "$out/omega/results.tsv" > "$out/m" && cp "$out/m" "$out/omega/results.tsv"
184echo stale > "$out/omega/st3-exit/work/NOTES.md"; echo stale > "$out/omega/st3-exit/work/stale.om"
185run omega "$out/omega" "$home"
186check "resume exit status" "$?" 0
187check "resume re-runs the task with no row" "$(field "$out/omega/results.tsv" st3-exit)" pass
188check "re-run task starts from a clean directory" "$(ls "$out/omega/st3-exit/work")" main.om
189check "re-run task starts with a fresh guard log" "$(col "$out/omega/results.tsv" st3-exit 7)" 2
190rm -rf "$out/sub" && cp -r "$out/omega" "$out/sub" && grep -v "^st3-exit$tab" "$out/omega/results.tsv" > "$out/sub/results.tsv"
191printf 'st3-exit-old\tpass\t0\t0\t0\t0\t0\t0\n' >> "$out/sub/results.tsv"
192run omega "$out/sub" "$home"
193check "resume matches task ids exactly (st3-exit-old does not stand for st3-exit)" "$(field "$out/sub/results.tsv" st3-exit)" pass
194PATH="$here/selftest:$PATH" OMEGA_HOME="$home" OMEGA_EVAL_MODEL=m OMEGA_EVAL_SEED=none sh "$here/run.sh" omega "$out/bogus" "$here/selftest/tasks" st1-pass st9-bogus > "$out/bogus.log" 2>&1
195check "an id with no task directory is refused" "$?" 2
196check "an id with no task directory wrote nothing" "$([ -e "$out/bogus" ] && echo yes || echo no)" no
197mkdir -p "$out/legacy" && printf 'id\tverdict\nst1-pass\twrong-output\n' > "$out/legacy/results.tsv"
198run omega "$out/legacy" "$home"
199check "rows without a manifest refused" "$?" 2
200check "rows without a manifest: reason" "$(grep -c 'has rows but no manifest' "$out/legacy.log")" 1
201check "rows without a manifest: no manifest written" "$([ -e "$out/legacy/manifest.tsv" ] && echo yes || echo no)" no
202check "resume rows" "$(rows "$out/omega")" "$ntasks"
203run omega "$out/omega" "$home"
204check "second resume exit status" "$?" 0
205check "second resume adds no row" "$(rows "$out/omega")" "$ntasks"
206refused() { # refused NAME, after a run into $out/r with exit status $r
207 check "resume with changed $1 refused" "$r" 2
208 check "resume with changed $1: reason" "$(grep -c 'resume with different inputs' "$out/r.log")" 1
209 check "resume with changed $1 added no row" "$(rows "$out/r")" "$ntasks"
210}
211fresh() { rm -rf "$out/r" && cp -r "$out/omega" "$out/r"; }
212fresh; run omega "$out/r" "$home" other-model; r=$?; refused model
213fresh; run omega "$out/r" "$home" selftest-stub 7; r=$?; refused seed
214fresh; copyhome; printf x >> "$out/h/gate"; run omega "$out/r" "$out/h"; r=$?; refused gate
215fresh; copyhome; echo x > "$out/h/seed/qualification/om2/fixtures/selftest-extra"; run omega "$out/r" "$out/h"; r=$?; refused fixtures
216fresh; copyhome; echo x >> "$out/h/context/OMEGA.md"; run omega "$out/r" "$out/h"; r=$?; refused prompt
217fresh; copyhome; echo '// x' >> "$out/h/omega-guard.ts"; run omega "$out/r" "$out/h"; r=$?
218check "a guard that is not this repository's refused" "$r" 2
219check "a guard that is not this repository's: reason" "$(grep -c 'omega-guard.ts' "$out/r.log")" 1
220check "a guard that is not this repository's added no row" "$(rows "$out/r")" "$ntasks"
221fresh; copyhome; echo '99 x' >> "$out/h/context/errors.tsv"; run omega "$out/r" "$out/h"; r=$?; refused errors
222fresh; rm -rf "$out/t" && cp -r "$here/selftest/tasks" "$out/t" && echo x >> "$out/t/st1-pass/spec.md"
223run omega "$out/r" "$home" selftest-stub none "$out/t"; r=$?; refused tasks
224mktree() {
225 rm -rf "$out/tree" && mkdir -p "$out/tree/coder/eval" && cp "$here/../../PIN" "$out/tree/PIN" &&
226 cp "$here/../omega-guard.ts" "$out/tree/coder/" &&
227 cp "$here/run.sh" "$here/case.sh" "$here/score.sh" "$here/cat.om" "$out/tree/coder/eval/"
228}
229fresh; mktree; echo '# x' >> "$out/tree/coder/eval/run.sh"
230run omega "$out/r" "$home" selftest-stub none "" "$out/tree/coder/eval/run.sh"; r=$?; refused runner
231fresh; mktree; echo '# x' >> "$out/tree/coder/eval/case.sh"
232run omega "$out/r" "$home" selftest-stub none "" "$out/tree/coder/eval/run.sh"; r=$?; refused "case.sh"
233fresh; mktree; echo '# x' >> "$out/tree/coder/eval/score.sh"
234run omega "$out/r" "$home" selftest-stub none "" "$out/tree/coder/eval/run.sh"; r=$?; refused "score.sh"
235fresh; rm -rf "$out/stub" && cp -r "$here/selftest" "$out/stub" && echo '# x' >> "$out/stub/aien"
236run omega "$out/r" "$home" selftest-stub none "" "" "$out/stub"; r=$?; refused "session program"
237
238# the verdict words run.sh and score.sh can write are exactly case.sh's VERDICTS (compare.sh and refcheck.sh read
239# case.sh)
240check "run.sh and score.sh verdicts are case.sh VERDICTS" \
241 "$({ grep -o 'verdict=[a-z-][a-z-]*' "$here/run.sh" | sed 's/verdict=//'; grep -o 'SCORE \$id [a-z-][a-z-]*' "$here/score.sh" | cut -d' ' -f3; } | LC_ALL=C sort -u | tr '\n' ' ')" \
242 "$(echo $VERDICTS | tr ' ' '\n' | LC_ALL=C sort | tr '\n' ' ')"
243
244# a program that copies the task's expected output by its absolute path: the narrow sandbox shows it nothing,
245# and the same scorer with the old read-only root shows it the answer (the negative control)
246check "cheat scores wrong-output (omega)" "$(field "$out/omega/results.tsv" st7-cheat)" wrong-output
247check "cheat's own check scores wrong-output" "$(sed -n 's/^check_score //p' "$out/omega/st7-cheat/session.txt")" wrong-output
248mktree; sed -i 's|bwrap --ro-bind /usr /usr|bwrap --ro-bind / / --ro-bind /usr /usr|' "$out/tree/coder/eval/case.sh"
249check "mutant scorer: read-only root inserted" "$(grep -c 'bwrap --ro-bind / / ' "$out/tree/coder/eval/case.sh")" 1
250SELFTEST_TASKS="$here/selftest/tasks" PATH="$here/selftest:$PATH" OMEGA_HOME="$home" OMEGA_EVAL_MODEL=selftest-stub OMEGA_EVAL_SEED=none \
251 sh "$out/tree/coder/eval/run.sh" omega "$out/mut" "$here/selftest/tasks" st7-cheat > "$out/mut.log" 2>&1
252check "mutant scorer: run exit status" "$?" 0
253check "mutant scorer lets the cheat pass (the test bites)" "$(field "$out/mut/results.tsv" st7-cheat)" pass
254# the guard's omega_run sandbox: the session directory is readable, the context pack and the directory above are not
255if command -v node > /dev/null 2>&1; then
256 gb="$out/gs/session"
257 mkdir -p "$gb" "$out/gs/tmp" && echo inside > "$gb/inside.txt" && echo outside > "$out/gs/outside.txt"
258 for p in "$gb/inside.txt" "$home/context/OMEGA.md" "$gb/../outside.txt"; do
259 sed "s|@TASKS@/st7-cheat/expected.out|$p|" "$here/selftest/src/st7-cheat.om" | "$home/omc" > "$out/gs/peek" 2> /dev/null && chmod +x "$out/gs/peek"
260 TMPDIR="$out/gs/tmp" node --no-warnings --input-type=module -e '
261 const { sandboxRun } = await import(process.argv[1]);
262 const r = sandboxRun(process.argv[3], process.argv[2], [], "");
263 process.stdout.write(String(r.out ?? ""));' "$here/../omega-guard.ts" "$gb" "$out/gs/peek" > "$out/gs/got" 2> "$out/gs/err"
264 echo "$p $(wc -c < "$out/gs/got" | tr -d ' ')" >> "$out/gs/seen"
265 done
266 check "guard sandbox: a program reads the session directory" "$(sed -n 1p "$out/gs/seen")" "$gb/inside.txt 7"
267 check "guard sandbox: a program cannot read the context pack" "$(sed -n 2p "$out/gs/seen")" "$home/context/OMEGA.md 0"
268 check "guard sandbox: a program cannot read the directory above" "$(sed -n 3p "$out/gs/seen")" "$gb/../outside.txt 0"
269else
270 check "node on PATH for the guard sandbox check" no yes
271fi
272
273# the scorer: verdicts the program earns, and machine failures that stop the run
274fakegate() { # fakegate VERDICT-TEXT EXIT [FINAL-LINE]: a gate that answers every case with that verdict
275 copyhome
276 printf '#!/bin/sh\nid=$(sed -n 2p "$3" | cut -d" " -f1)\nmkdir -p "$4" && : > "$4/2.out"\necho "VERDICT $id %s"\n%s\nexit %s\n' \
277 "$1" "${3:+echo \"$3\"}" "$2" > "$out/h/gate"
278 chmod +x "$out/h/gate"
279}
280fakegate "INVALID child deadline exceeded, sent SIGKILL; wait low byte=9" 3 "OM2GATE INVALID cases=1"
281run omega "$out/g1" "$out/h"
282check "gate deadline: run exit status" "$?" 0
283check "gate deadline scores timeout" "$(field "$out/g1/results.tsv" st1-pass)" timeout
284check "gate deadline: compile-fail stays compile-fail" "$(field "$out/g1/results.tsv" st4-badcompile)" compile-fail
285fakegate "INVALID child signal; wait low byte=11" 3 "OM2GATE INVALID cases=1"
286run omega "$out/g2" "$out/h"
287check "gate signal scores crash" "$(field "$out/g2/results.tsv" st1-pass)" crash
288# the scorer's gate runs in the narrow sandbox: a write outside it fails, and it sees no task file, context pack
289# or /proc
290copyhome
291printf '#!/bin/sh\nid=$(sed -n 2p "$3" | cut -d" " -f1)\ntouch "%s" 2> /dev/null\nfor f in "%s" "%s" /proc/self/status; do [ -e "$f" ] && echo "$f"; done >> "$(dirname "$4")/seen"\nmkdir -p "$4" && : > "$4/2.out"\necho "VERDICT $id PASS run exit=0 expected=0 stdout=match stderr=match"\necho "OM2GATE PASS cases=1"\nexit 0\n' \
292 "$out/escaped" "$here/selftest/tasks/st1-pass/expected.out" "$out/h/context/OMEGA.md" > "$out/h/gate"
293chmod +x "$out/h/gate"
294run omega "$out/g6" "$out/h"
295check "sandboxed gate: run exit status" "$?" 0
296check "sandboxed gate: verdict" "$(field "$out/g6/results.tsv" st1-pass)" pass
297check "sandboxed gate cannot write outside its sandbox" "$([ -e "$out/escaped" ] && echo yes || echo no)" no
298check "sandboxed gate ran both stages" "$([ -f "$out/g6/st1-pass/sandbox/a/run/seen" ] && [ -f "$out/g6/st1-pass/sandbox/b/run/seen" ] && echo yes)" yes
299check "sandboxed gate sees no task file, context pack or /proc" "$(cat "$out/g6/st1-pass/sandbox/a/run/seen" "$out/g6/st1-pass/sandbox/b/run/seen")" ""
300# score.sh on st3-exit (stdout "three\n", exit 3) with the real gate
301mkdir -p "$out/sc"
302sc() { # sc HOME SRC-NAME: score selftest/src/SRC-NAME.om on st3-exit into $out/sc/SRC-NAME; prints score.sh's exit
303 sh "$here/score.sh" "$1" "$here/selftest/src/$2.om" "$here/selftest/tasks/st3-exit" st3-exit "$out/sc/$2" > "$out/sc/$2.log" 2>&1
304 echo $?
305}
306scored() { sed -n 's/^SCORE st3-exit \([a-z-]*\) .*/\1/p' "$out/sc/$1.log"; }
307check "score.sh: exit 127 is the program's" "$(sc "$home" sc-e127) $(scored sc-e127)" "1 wrong-output"
308check "score.sh: exit 127 ran the exec control" "$(grep -c '^VERDICT st3-exit [A-Z]* run exit=0 ' "$out/sc/sc-e127/c.log")" 1
309fakegate "INVALID exec or redirect failed (child exit 127)" 1 "OM2GATE INVALID cases=1"
310check "score.sh: exit 127 from a gate that cannot exec is the machine's" "$(sc "$out/h" sc-e127)" 3
311check "score.sh: right stdout, wrong exit" "$(sc "$home" sc-rightout) $(scored sc-rightout)" "1 wrong-output"
312check "score.sh: stage A says nothing about stdout" "$(grep -c 'stdout differs\|stdout=match' "$out/sc/sc-rightout.log") $(grep -c 'stdout=(compared in stage B)' "$out/sc/sc-rightout.log")" "0 1"
313check "score.sh: a 2.5 MiB writer scores crash" "$(sc "$home" sc-big) $(scored sc-big)" "1 crash"
314check "score.sh: its stdout stopped at the file-size limit" "$(wc -c < "$out/sc/sc-big/a/run/s/2.out" | tr -d ' ')" "$((CASE_FSIZE_BLOCKS * 512))"
315check "score.sh: st3-exit's own program passes" "$(sc "$home" st3-exit) $(scored st3-exit)" "0 pass"
316check "score.sh: stage A inputs are cases.txt, main.om and stdin" "$(ls "$out/sc/st3-exit/a/in" | tr '\n' ' ')" "cases.txt main.om stdin "
317check "score.sh: a relative OMEGA_HOME passes the same program" \
318 "$(cd "$(dirname "$home")" && sh "$here/score.sh" "./$(basename "$home")" "$here/selftest/src/st3-exit.om" "$here/selftest/tasks/st3-exit" st3-exit "$out/sc/rel" 2>&1 | sed -n 's/^SCORE st3-exit \([a-z-]*\) .*/\1/p')" pass
319copyhome; printf '#!/bin/sh\nexit 0\n' > "$out/h/gate"; chmod +x "$out/h/gate"
320run omega "$out/g3" "$out/h"
321check "gate with no verdict stops the run" "$?" 3
322check "gate with no verdict wrote no row" "$(rows "$out/g3")" 0
323fakegate "PASS run exit=0 expected=0 stdout=match stderr=match" 1 "OM2GATE FAIL cases=1"
324run omega "$out/g4" "$out/h"
325check "gate PASS line with exit 1 stops the run" "$?" 3
326check "gate PASS line with exit 1 wrote no row" "$(rows "$out/g4")" 0
327rm -rf "$out/t" && cp -r "$here/selftest/tasks" "$out/t" && rm "$out/t/st1-pass/expected.out" && mkdir "$out/t/st1-pass/expected.out"
328run omega "$out/g5" "$home" selftest-stub none "$out/t"
329check "unreadable fixture stops the run" "$?" 3
330check "unreadable fixture: reason" "$(grep -c 'scorer gave no verdict' "$out/g5.log")" 1
331check "unreadable fixture wrote no row" "$(rows "$out/g5")" 0
332copyhome; chmod -x "$out/h/omc"; run omega "$out/x1" "$out/h"
333check "omc not executable stops the run" "$?" 3
334check "omc not executable wrote no row" "$(rows "$out/x1")" 0
335copyhome; chmod -x "$out/h/gate"; run omega "$out/x2" "$out/h"
336check "gate not executable stops the run" "$?" 3
337check "gate not executable wrote no row" "$(rows "$out/x2")" 0
338for mode in nolog nostop nocheck noreply; do
339 rm -rf "$out/stub" && cp -r "$here/selftest" "$out/stub" && echo "$mode" > "$out/stub/mode"
340 run omega "$out/m$mode" "$home" selftest-stub none "" "" "$out/stub"
341 check "stub mode $mode stops the run" "$?" 3
342 check "stub mode $mode wrote no row" "$(rows "$out/m$mode")" 0
343done
344for rc in 1 124; do
345 rm -rf "$out/stub" && cp -r "$here/selftest" "$out/stub" && sed -i "s/^exit 0\$/exit $rc/" "$out/stub/aien"
346 check "stub edited to exit $rc" "$(grep -c "^exit $rc\$" "$out/stub/aien")" 1
347 run omega "$out/s$rc" "$home" selftest-stub none "" "" "$out/stub"
348 echo $? > "$out/s$rc.rc"
349done
350check "session rc 1 stops the run" "$(cat "$out/s1.rc")" 3
351check "session rc 1 wrote no row" "$(rows "$out/s1")" 0
352check "session rc 124: run exit status" "$(cat "$out/s124.rc")" 0
353check "session rc 124 is scored" "$(field "$out/s124/results.tsv" st1-pass)" pass
354check "session rc 124 recorded" "$(col "$out/s124/results.tsv" st1-pass 5)" 124
355
356# refusals before anything is written
357run omega "$out/nomodel" "$home" ""
358check "missing model refused" "$?" 2
359check "missing model wrote nothing" "$([ -e "$out/nomodel" ] && echo yes || echo no)" no
360run omega "$out/noseed" "$home" selftest-stub ""
361check "missing seed refused" "$?" 2
362check "missing seed wrote nothing" "$([ -e "$out/noseed" ] && echo yes || echo no)" no
363mkdir -p "$out/nojq"
364for d in $(echo "$PATH" | tr ':' ' '); do
365 [ -d "$d" ] || continue
366 for f in "$d"/*; do b=$(basename "$f"); [ "$b" = jq ] || [ -e "$out/nojq/$b" ] || [ -L "$out/nojq/$b" ] || ln -s "$f" "$out/nojq/$b"; done
367done
368run omega "$out/jqrun" "$home" selftest-stub none "" "" "" "$out/nojq"
369check "missing jq refused" "$?" 2
370check "missing jq: reason" "$(grep -c 'jq not on PATH' "$out/jqrun.log")" 1
371check "missing jq wrote nothing" "$([ -e "$out/jqrun" ] && echo yes || echo no)" no
372
373rm -rf "$out/t" && cp -r "$here/selftest/tasks" "$out/t" && echo x > "$out/t/st1-pass/a b"
374run omega "$out/ws" "$home" selftest-stub none "$out/t"
375check "a task file name with white space is refused" "$?" 2
376check "white space refusal: reason" "$(grep -c 'holds white space' "$out/ws.log")" 1
377check "white space refusal wrote no results" "$([ -e "$out/ws/results.tsv" ] && echo yes || echo no)" no
378
379mkdir -p "$out/pin"
380cp -r "$home/context" "$out/pin/context"
381cp "$home/gate" "$home/omc" "$out/pin/"
382sed 's/^omc-sha256 .*/omc-sha256 0000000000000000000000000000000000000000000000000000000000000000/' "$home/context/PIN" > "$out/pin/context/PIN"
383run omega "$out/pinrun" "$out/pin"
384check "pack built for another pin refused" "$?" 2
385check "pack built for another pin: reason" "$(grep -c 'pack built for another pin' "$out/pinrun.log")" 1
386check "pack built for another pin wrote nothing" "$([ -e "$out/pinrun" ] && echo yes || echo no)" no
387
388mkdir -p "$out/wrong"
389cp -r "$home/context" "$out/wrong/context"
390cp "$home/gate" "$home/omc" "$out/wrong/"
391printf '\0' >> "$out/wrong/omc"
392run omega "$out/wrongrun" "$out/wrong"
393check "wrong compiler refused" "$?" 2
394check "wrong compiler: reason" "$(grep -c 'pinned omc mismatch' "$out/wrongrun.log")" 1
395check "wrong compiler wrote nothing" "$([ -e "$out/wrongrun" ] && echo yes || echo no)" no
396
397# refcheck.sh on three synthetic tasks whose references are selftest/src
398mkdir -p "$out/rt" "$out/rr"
399for id in st1-pass st3-exit st6-stdin; do cp -r "$here/selftest/tasks/$id" "$out/rt/"; cp "$here/selftest/src/$id.om" "$out/rr/"; done
400mkman() { # mkman: tasks.json for $out/rt with the sha256 of each reference in $out/rr
401 for id in st1-pass st3-exit st6-stdin; do
402 jq -n --arg id "$id" --rawfile spec "$out/rt/$id/spec.md" --arg e "$(sha "$out/rt/$id/expected.out")" \
403 --argjson n "$(wc -c < "$out/rt/$id/stdin")" --argjson x "$(cat "$out/rt/$id/expected.exit")" --arg r "$(sha "$out/rr/$id.om")" \
404 '{id: $id, spec: ($spec | rtrimstr("\n")), stdin_bytes: $n, expected_sha256: $e, exit: $x, ref_sha256: $r}'
405 done | jq -s . > "$out/rt.json"
406}
407refc() { OMEGA_HOME="${1:-$home}" sh "$here/refcheck.sh" "$out/rt" "$out/rt.json" "$out/rr" > "$out/ref.log" 2>&1; echo $?; }
408mkman
409check "refcheck passes good references" "$(refc)" 0
410check "refcheck PASS line" "$(grep -c '^REFCHECK PASS n=3 ' "$out/ref.log")" 1
411cp "$here/selftest/src/st2-wrong.om" "$out/rr/st1-pass.om"; mkman
412check "refcheck refuses a wrong reference" "$(refc)" 1
413cp "$here/selftest/src/st1-pass.om" "$out/rr/st1-pass.om"
414check "refcheck refuses a reference whose sha256 is not the manifest's" "$(refc)" 1
415# each manifest field on its own (the gate still passes, so only the metadata check can refuse)
416for edit in '.expected_sha256 = "0"' '.stdin_bytes = 99' '.exit = 9' '.spec = "other"' '.id = "st9-none"'; do
417 mkman; jq "map(if .id == \"st3-exit\" then $edit else . end)" "$out/rt.json" > "$out/rt2.json" && mv "$out/rt2.json" "$out/rt.json"
418 check "refcheck refuses a manifest with $edit for one task" "$(refc)" 1
419done
420head -c "$((CASE_FSIZE_BLOCKS * 256))" /dev/zero > "$out/rt/st1-pass/expected.out"; mkman
421check "refcheck alarms on an expected output at half the file-size limit" "$(refc) $(grep -c 'st1-pass expected.out is .* half or more' "$out/ref.log")" "1 1"
422cp "$here/selftest/tasks/st1-pass/expected.out" "$out/rt/st1-pass/expected.out"
423mkman
424check "refcheck passes again with the manifest restored" "$(refc)" 0
425# fake gates: refcheck must want every case to pass, the case count to be n, and exit 0
426fg() { copyhome; printf '#!/bin/sh\n%s\n' "$1" > "$out/h/gate"; chmod +x "$out/h/gate"; }
427allpass='for id in st1-pass st3-exit st6-stdin; do echo "VERDICT $id PASS"; done'
428fg "$allpass; echo 'OM2GATE PASS cases=3 '; exit 0"
429check "refcheck with a fake gate that passes all three" "$(refc "$out/h")" 0
430fg "$allpass; echo 'OM2GATE PASS cases=2 '; exit 0"
431check "refcheck refuses a gate case count other than n" "$(refc "$out/h")" 1
432fg "$allpass; echo 'OM2GATE PASS cases=3 '; exit 1"
433check "refcheck refuses a PASS line with gate exit 1" "$(refc "$out/h")" 1
434fg "echo 'VERDICT st1-pass FAIL'; echo 'VERDICT st3-exit PASS'; echo 'VERDICT st6-stdin PASS'; echo 'OM2GATE PASS cases=3 '; exit 0"
435check "refcheck refuses a FAIL verdict even under a PASS line" "$(refc "$out/h")" 1
436
437if [ "$fail" -eq 0 ]; then echo "SELFTEST PASS"; else echo "SELFTEST FAIL"; cat "$out/omega.log"; fi
438exit "$fail"