interplane.Get Omega
Read in your language

omega-mv

Omega: a language for verified programs, semantic identity and machine realization. Seed-born work and existing implementations have distinct provenance.

Brought by drakestapleton

branch main Branches Tags History

omega-mv / research / om-bench / controls / run-controls.sh

207 lines · 12080 bytes Raw Blame History
1#!/bin/sh
2# om-bench (#653) negative and positive controls for the driver and for gate.om.
3# Each control asserts its diagnostic text, not only an exit status.
4# Usage: run-controls.sh OUTDIR (OUTDIR must not exist)
5set -u
6HERE=$(cd "$(dirname "$0")" && pwd)
7BENCH=$HERE/../bench
8REPO=${OMB_REPO:-$(cd "$HERE/../../.." && pwd)}
9# Host-only driver regressions use synthetic subprocesses and in-memory tasks.
10# They never compile or execute a candidate, call a model, or read sealed data.
11# Usage: run-controls.sh --host-only (no Spark/compiler required)
12if [ "${1:-}" = "--host-only" ]; then
13 PYTHONDONTWRITEBYTECODE=1 python3 - "$REPO" <<'PY_HOST_CONTROLS'
14import contextlib, importlib.util, io, json, os, pathlib, subprocess, sys, tempfile
15ROOT = pathlib.Path(sys.argv[1]).resolve()
16SCRIPT = ROOT / 'research/om-bench/run-arms.sh'
17MOD = ROOT / 'research/om-bench/bench/ombench.py'
18checks = 0
19
20def check(ok, name):
21 global checks
22 assert ok, name
23 checks += 1
24 print('PASS', name)
25
26# Exercise actual shell script with fake python3 and gate, never candidate/compiler execution.
27with tempfile.TemporaryDirectory() as tmp:
28 t = pathlib.Path(tmp); bin = t/'bin'; bin.mkdir(); run=t/'run'; run.mkdir()
29 (bin/'python3').write_text('''#!/bin/sh
30stage=$2
31echo "$stage" >> "$TRACE"
32if [ "$stage" = "$FAIL_STAGE" ]; then exit 17; fi
33exit 0
34'''); (bin/'python3').chmod(0o755)
35 (run/'gate').write_text('''#!/bin/sh
36echo gate >> "$TRACE"
37echo "OM2GATE FAIL cases=1 pass=0 fail=1 invalid=0 not_run=0"
38exit "$GATE_RC"
39'''); (run/'gate').chmod(0o755)
40 for stage, gate_rc, want_rc, want_trace in [
41 ('gen',0,17,['gen']),('judge',0,17,['gen','judge']),
42 ('gatelist',0,17,['gen','judge','gatelist']),
43 ('',91,91,['gen','judge','gatelist','gate']),
44 ('crosscheck',0,17,['gen','judge','gatelist','gate','crosscheck']),
45 ('summary',0,17,['gen','judge','gatelist','gate','crosscheck','summary']),
46 *[('',rc,0,['gen','judge','gatelist','gate','crosscheck','summary']) for rc in (0,1,2,3)],
47 ]:
48 trace=t/'trace'; trace.write_text('')
49 env=dict(os.environ, PATH=str(bin)+':'+os.environ['PATH'], TRACE=str(trace),FAIL_STAGE=stage,GATE_RC=str(gate_rc),OMB_REPO=str(t))
50 r=subprocess.run(['sh',str(SCRIPT),str(run),'dev','a'],env=env,capture_output=True,text=True)
51 check(r.returncode==want_rc and trace.read_text().splitlines()==want_trace, f'shell {stage or "completed gate"} rc={gate_rc}: exit={r.returncode}')
52 if want_rc:
53 check(f'rc={want_rc}' in (run/'a-dev/status.txt').read_text(), 'child failure logged')
54 (run/'a-dev/summary-dev.json').write_text('prior summary')
55 trace.write_text('')
56 r=subprocess.run(['sh',str(SCRIPT),str(run),'dev','a'],env=env,capture_output=True,text=True)
57 check(r.returncode==1 and trace.read_text()=='' and (run/'a-dev/summary-dev.json').read_text()=='prior summary','runner refuses stale summary without touching it')
58 trace.write_text(''); env.update(FAIL_STAGE='',GATE_RC='91')
59 r=subprocess.run(['sh',str(SCRIPT),str(run),'dev','fpy'],env=env,capture_output=True,text=True)
60 check(r.returncode==0 and trace.read_text().splitlines()==['gen','judge','summary'], 'foreign arm does not cross-judge')
61
62# Import functions only, with sealed path pointed to an empty temporary directory.
63with tempfile.TemporaryDirectory() as tmp:
64 t=pathlib.Path(tmp); os.environ['OMB_SEALED']=str(t/'no-sealed-data')
65 spec=importlib.util.spec_from_file_location('ombench', MOD); mod=importlib.util.module_from_spec(spec); spec.loader.exec_module(mod)
66 check(mod.system_prompt('om').endswith((ROOT/'research/om-bench/REFERENCE-CARD.md').read_text()), 'Omega prompt loads unchanged public card')
67 check(mod.system_prompt('py')=='You write complete Python 3 programs.', 'foreign prompt unchanged')
68 mod.task_dirs=lambda split: (str(t),['ctl-synthetic'])
69 mod.load_task=lambda split,tid: {'tid':tid,'family':'ctl-synthetic','cases':[{'n':0}]}
70 row={'tid':'ctl-synthetic','family':'ctl-synthetic','code_sha256':'synthetic','class':'pass','cases':[{'n':0,'status':'pass'}]}
71 vp=t/'verdicts-dev.jsonl'; log=t/'gate.log'
72 def report(body, cls='pass', status='pass', code='synthetic'):
73 row.update({'class':cls,'cases':[{'n':0,'status':status}],'code_sha256':code});vp.write_text(json.dumps(row)+'\n');log.write_text(body)
74 good='VERDICT ctl-synthetic.00 PASS\nOM2GATE PASS cases=1 pass=1 fail=0 invalid=0 not_run=0\n'
75 def cross():
76 with contextlib.redirect_stdout(io.StringIO()): return mod.crosscheck(str(t),'dev',str(log))
77 report(good);check(cross()==0,'completed matching gate accepted')
78 report('VERDICT ctl-synthetic.00 FAIL\nOM2GATE FAIL cases=1 pass=0 fail=1 invalid=0 not_run=0\n','wrong_output','wrong_output');check(cross()==0,'wrong candidate remains failure in denominator')
79 report('VERDICT ctl-synthetic.nocode NOT_RUN\nOM2GATE NOT_RUN cases=1 pass=0 fail=0 invalid=0 not_run=1\n','no_code','',None);check(cross()==0,'no-code result retained')
80 report('VERDICT ctl-synthetic.00 INVALID\nOM2GATE INVALID cases=1 pass=0 fail=0 invalid=1 not_run=0\n','runtime_mechanical','timeout');check(cross()==0,'candidate timeout remains mechanical outcome')
81 for body in ['',good.replace('OM2GATE PASS','OM2GATE FAIL'),good.replace('cases=1','cases=2'),good.replace('pass=1','pass=0'),good.replace('VERDICT ctl-synthetic.00 PASS\n','VERDICT ctl-synthetic.00 PASS\nVERDICT ctl-synthetic.00 PASS\n'),good.split('OM2GATE')[0],good.replace('PASS\n','FAIL\n',1),good+'garbage\n','IDENTITY MISMATCH: every case is NOT_RUN\n'+good]:
82 report(body);check(cross()!=0,'incomplete/disagreeing/mismatched gate rejected')
83 try:
84 with contextlib.redirect_stdout(io.StringIO()): mod.summary(str(t),'dev','a')
85 except SystemExit: pass
86 else: raise AssertionError('summary accepted invalid gate')
87 check(not (t/'summary-dev.json').exists(),'standalone summary refuses invalid gate')
88 report('OM2GATE NOT_RUN cases=1 pass=0 fail=0 invalid=0 not_run=1\n','no_code','',None)
89 check(cross()!=0, 'all-no-code missing verdict refused')
90 report(good)
91 with contextlib.redirect_stdout(io.StringIO()): mod.summary(str(t),'dev','a')
92 check(json.loads((t/'summary-dev.json').read_text())['totals']['classes']=={'pass':1},'valid summary still works')
93 prior=(t/'summary-dev.json').read_bytes()
94 report('')
95 try:
96 mod.summary(str(t),'dev','a')
97 except SystemExit: pass
98 else: raise AssertionError('summary overwrote existing artifact')
99 check((t/'summary-dev.json').read_bytes()==prior,'summary preserves existing artifact on refusal')
100print('PASS synthetic host controls:',checks)
101PY_HOST_CONTROLS
102 exit $?
103fi
104OMC=$REPO/seed/out/omc
105SHA=2c82480596bbdc50cda9626866209ef662d5834f278f839ff4ce0250c495029f
106OUT=$1
107[ -e "$OUT" ] && { echo "OUTDIR exists"; exit 91; }
108mkdir -p "$OUT"
109FAILS=0
110ok() { echo "CONTROL PASS $1"; }
111bad() { echo "CONTROL FAIL $1"; FAILS=$((FAILS+1)); }
112
113# Task tree: one split "ctl", each task a copy of a public dev task.
114T=$OUT/tasks/ctl
115mkdir -p "$T"
116mk() { cp -r "$BENCH/pubtasks/dev/$2" "$T/$1"; }
117for n in ref mut empty trap slow e13 authz nocode; do mk ctl-fibo-$n fibo.v1; done
118for n in ref mut; do mk ctl-brk-$n brackets.v1; done
119
120R=$OUT/run-om
121place() { # place TID SRCFILE LANG
122 d=$R/cand/$1; mkdir -p "$d"
123 case $3 in om) f=prog.om;; py) f=prog.py;; c) f=prog.c;; esac
124 cp "$2" "$d/$f"
125 s=$(sha256sum "$2" | cut -d' ' -f1)
126 printf '{"tid":"%s","code_sha256":"%s","attempts":[]}\n' "$1" "$s" > "$d/gen.json"
127}
128for n in ref mut empty trap slow e13 authz; do place ctl-fibo-$n "$HERE/fibo-$n.om" om; done
129for n in ref mut; do place ctl-brk-$n "$HERE/brk-$n.om" om; done
130mkdir -p "$R/cand/ctl-fibo-nocode"
131printf '{"tid":"ctl-fibo-nocode","code_sha256":null,"attempts":[]}\n' > "$R/cand/ctl-fibo-nocode/gen.json"
132
133export OMB_TASKROOT=$OUT/tasks
134python3 "$BENCH/ombench.py" judge --run "$R" --split ctl --arm a > "$OUT/judge-om.txt" 2>&1
135echo "judge rc=$?" >> "$OUT/judge-om.txt"
136expect() { # expect RUN TID CLASS
137 got=$(python3 -c "import json,sys
138for l in open(sys.argv[1]):
139 r=json.loads(l)
140 if r['tid']==sys.argv[2]: print(r['class'])" "$1/verdicts-ctl.jsonl" "$2")
141 [ "$got" = "$3" ] && ok "$2 -> $3" || bad "$2 expected $3 got '$got'"
142}
143expect "$R" ctl-fibo-ref pass
144expect "$R" ctl-brk-ref pass
145expect "$R" ctl-fibo-mut wrong_output
146expect "$R" ctl-brk-mut wrong_output
147expect "$R" ctl-fibo-empty runtime_mechanical
148expect "$R" ctl-fibo-trap runtime_mechanical
149expect "$R" ctl-fibo-slow runtime_mechanical
150expect "$R" ctl-fibo-e13 compile_refused
151expect "$R" ctl-fibo-authz authz_violation
152expect "$R" ctl-fibo-nocode no_code
153
154# gate.om judges the same Omega candidates; crosscheck must agree case by case.
155"$OMC" < "$REPO/seed/qualification/om2/gate.om" > "$OUT/gate" && chmod +x "$OUT/gate" || bad "gate build"
156python3 "$BENCH/ombench.py" gatelist --run "$R" --split ctl > /dev/null
157(cd "$REPO" && "$OUT/gate" "$OMC" "$SHA" "$R/gate-ctl.cases" "$OUT/gate-scratch" > "$OUT/gate-ctl.log" 2>&1)
158echo "gate rc=$?" > "$OUT/gate-ctl.status"
159grep -q '^OM2GATE ' "$OUT/gate-ctl.log" && ok "gate final line present" || bad "gate final line missing"
160python3 "$BENCH/ombench.py" crosscheck --run "$R" --split ctl --gate-log "$OUT/gate-ctl.log" > "$OUT/crosscheck.txt" 2>&1
161rc=$?
162grep -q 'crosscheck agree=[1-9][0-9]* disagree=0' "$OUT/crosscheck.txt" && [ $rc -eq 0 ] \
163 && ok "gate.om and comparator agree" || bad "crosscheck rc=$rc $(head -3 "$OUT/crosscheck.txt")"
164
165# gate.om identity guard: a wrong compiler hash must make the gate INVALID with every case NOT_RUN.
166(cd "$REPO" && "$OUT/gate" "$OMC" 0000000000000000000000000000000000000000000000000000000000000000 \
167 "$R/gate-ctl.cases" "$OUT/gate-badsha" > "$OUT/gate-badsha.log" 2>&1)
168rc=$?
169grep -q '^OM2GATE INVALID' "$OUT/gate-badsha.log" && [ $rc -eq 3 ] && ok "wrong omc sha -> INVALID" \
170 || bad "wrong omc sha rc=$rc"
171
172# Denominator guard: a task with no candidate record is NOT_RUN and the judge exits 2, never a pass rate.
173R2=$OUT/run-missing; mkdir -p "$R2/cand"
174cp -r "$R/cand/ctl-fibo-ref" "$R2/cand/"
175python3 "$BENCH/ombench.py" judge --run "$R2" --split ctl --arm a > "$OUT/judge-missing.txt" 2>&1
176rc=$?
177grep -q '^NOT_RUN 9 of 10' "$OUT/judge-missing.txt" && [ $rc -eq 2 ] && ! grep -q '^judged' "$OUT/judge-missing.txt" \
178 && ok "missing candidates -> NOT_RUN 9 of 10, rc=2" || bad "denominator guard rc=$rc"
179
180# Corpus guards: a task without cases or without a hidden output file is a CORPUS FAULT.
181mkdir -p "$OUT/tasks-bad/ctl"; cp -r "$T/ctl-fibo-ref" "$OUT/tasks-bad/ctl/zz-nocases"
182printf '{"cases":[]}\n' > "$OUT/tasks-bad/ctl/zz-nocases/meta.json"
183OMB_TASKROOT=$OUT/tasks-bad python3 "$BENCH/ombench.py" judge --run "$R" --split ctl --arm a > "$OUT/fault1.txt" 2>&1
184rc=$?
185grep -q 'CORPUS FAULT \[zz-nocases\] no cases' "$OUT/fault1.txt" && [ $rc -eq 1 ] && ok "empty task -> CORPUS FAULT" \
186 || bad "empty task guard rc=$rc"
187rm -r "$OUT/tasks-bad/ctl/zz-nocases"; cp -r "$T/ctl-fibo-ref" "$OUT/tasks-bad/ctl/zz-noout"
188rm "$OUT/tasks-bad/ctl/zz-noout/05.out"
189OMB_TASKROOT=$OUT/tasks-bad python3 "$BENCH/ombench.py" judge --run "$R" --split ctl --arm a > "$OUT/fault2.txt" 2>&1
190rc=$?
191grep -q 'CORPUS FAULT \[zz-noout\] case 5 missing .out' "$OUT/fault2.txt" && [ $rc -eq 1 ] \
192 && ok "missing expected output -> CORPUS FAULT" || bad "missing output guard rc=$rc"
193
194# Python and C arms: correct references pass, a planted wrong program fails, a forbidden import is flagged.
195T3=$OUT/tasks3/ctl; mkdir -p "$T3"
196for n in ref wrong authz; do cp -r "$BENCH/pubtasks/dev/fibo.v1" "$T3/ctl-fibo-$n"; done
197for L in py c; do
198 R3=$OUT/run-$L; arm=f$L; R=$R3
199 for n in ref wrong authz; do place ctl-fibo-$n "$HERE/fibo-$n.$L" $L; done
200 OMB_TASKROOT=$OUT/tasks3 python3 "$BENCH/ombench.py" judge --run "$R3" --split ctl --arm $arm > "$OUT/judge-$L.txt" 2>&1
201 expect "$R3" ctl-fibo-ref pass
202 expect "$R3" ctl-fibo-wrong wrong_output
203 expect "$R3" ctl-fibo-authz authz_violation
204done
205
206echo "CONTROLS FAILS=$FAILS"
207[ $FAILS -eq 0 ]