cortex
aien-dev/cortex
Brought by drakestapleton
cortex / tests / parity / run.sh
| 1 | #!/bin/sh |
| 2 | # tests/parity/run.sh : wire parity gate, Omega Cortex against the cortex-rs oracle (spec/PARITY.md). |
| 3 | # sh tests/parity/run.sh SRC.db |
| 4 | # SRC.db is a copy of a cortex-rs database; it is opened read-only and never written. The gate |
| 5 | # 1. builds the corpus (tests/parity/gen.sh) and the Omega driver/comparator (pdrive, pcmp); |
| 6 | # 2. makes two fresh copies with sqlite3 .backup, imports one with build/cortex-import; |
| 7 | # 3. starts the oracle (cortex-rs, port 18180) on one copy and the Omega server (build/cortex, |
| 8 | # port 18190) on the imported log, both with an unreachable encoder (http://127.0.0.1:9); |
| 9 | # 4. drives the identical request sequence against each, compares answer by answer, stops both. |
| 10 | # Exit 0 only when every record is exact or differs only by the masks of spec/PARITY.md. |
| 11 | # Environment: CORTEX_ORACLE (default: cortex-rs on PATH), CORTEX_BIN (default build/cortex), |
| 12 | # CORTEX_IMPORT (default build/cortex-import), PARITY_WORK (default build/parity), |
| 13 | # PARITY_EVIDENCE (default evidence/parity; set to a scratch directory for a mutant run), |
| 14 | # PARITY_GEN (corpus generator, default tests/parity/gen.sh; tests/claims/gen.sh for the claims |
| 15 | # corpus). |
| 16 | set -u |
| 17 | SRC=${1:?usage: run.sh SRC.db} |
| 18 | R=$(cd "$(dirname "$0")/../.." && pwd) |
| 19 | P=$R/tests/parity |
| 20 | W=${PARITY_WORK:-$R/build/parity} |
| 21 | EV=${PARITY_EVIDENCE:-$R/evidence/parity} |
| 22 | BIN=${CORTEX_BIN:-$R/build/cortex} |
| 23 | IMP=${CORTEX_IMPORT:-$R/build/cortex-import} |
| 24 | ORA=${CORTEX_ORACLE:-$(command -v cortex-rs)} |
| 25 | OP=${PARITY_ORACLE_PORT:-18180} |
| 26 | SP=${PARITY_OMEGA_PORT:-18190} |
| 27 | TOK=paritytok |
| 28 | ENC=http://127.0.0.1:9 |
| 29 | fail() { echo "parity: $*" >&2; stop; exit 2; } |
| 30 | OPID= |
| 31 | SPID= |
| 32 | # The Omega acceptor notices the state process's end within 0.2 s and closes the listener; wait |
| 33 | # for that (up to 5 s) so a following run can bind the same port. |
| 34 | freeport() { |
| 35 | for i in 1 2 3 4 5 6 7 8 9 10; do |
| 36 | curl -s -o /dev/null --max-time 1 "http://127.0.0.1:$1/health" || return 0 |
| 37 | sleep 0.5 |
| 38 | done |
| 39 | return 1 |
| 40 | } |
| 41 | stop() { |
| 42 | [ -n "$OPID" ] && kill "$OPID" 2>/dev/null |
| 43 | [ -n "$SPID" ] && kill "$SPID" 2>/dev/null |
| 44 | [ -n "$OPID$SPID" ] && { freeport $OP; freeport $SP; } |
| 45 | OPID= |
| 46 | SPID= |
| 47 | } |
| 48 | [ -f "$SRC" ] || fail "no source database $SRC" |
| 49 | [ -x "$BIN" ] && [ -x "$IMP" ] || fail "build first (sh build.sh)" |
| 50 | [ -n "$ORA" ] && [ -x "$ORA" ] || fail "oracle cortex-rs not found (set CORTEX_ORACLE)" |
| 51 | for port in $OP $SP; do |
| 52 | freeport $port || fail "port $port is in use" |
| 53 | done |
| 54 | OMC=$(sh "$R/tools/omc.sh") || fail "pinned omc unavailable" |
| 55 | rm -rf "$W" |
| 56 | mkdir -p "$W" "$EV" || fail "cannot make $W" |
| 57 | # private socket in a short directory: LOG.sock under a deep work directory can pass the 108-byte |
| 58 | # sockaddr_un path (a kernel boundary; the server refuses past 100 bytes) |
| 59 | SOCKD=$(mktemp -d /tmp/cxsock.XXXXXX) || fail "cannot make a socket directory" |
| 60 | trap 'rm -rf "$SOCKD"' EXIT |
| 61 | for u in pdrive pcmp; do |
| 62 | { echo om2; cat "$P/$u.om"; } > "$W/$u.om" |
| 63 | "$OMC" < "$W/$u.om" > "$W/$u" 2> "$W/$u.err" |
| 64 | if [ $? -ne 0 ] || [ -s "$W/$u.err" ]; then fail "$u.om did not compile"; fi |
| 65 | chmod +x "$W/$u" |
| 66 | done |
| 67 | sh "${PARITY_GEN:-$P/gen.sh}" "$SRC" "$W/corpus.tsv" > "$W/gen.out" || fail "corpus generation failed" |
| 68 | sqlite3 "file:$SRC?mode=ro" ".backup '$W/o.db'" || fail "copy failed" |
| 69 | sqlite3 "file:$SRC?mode=ro" ".backup '$W/i.db'" || fail "copy failed" |
| 70 | "$IMP" "$W/i.db" "$W/cx.log" > "$W/import.out" 2>&1 || fail "import failed: $(cat "$W/import.out")" |
| 71 | CORTEX_TOKEN=$TOK setsid nohup "$ORA" --port $OP --db-path "$W/o.db" --encoder-url $ENC \ |
| 72 | > "$W/oracle.out" 2>&1 < /dev/null & |
| 73 | OPID=$! |
| 74 | CORTEX_TOKEN=$TOK setsid nohup "$BIN" --port $SP --log "$W/cx.log" --sock "$SOCKD/s" --encoder-url $ENC \ |
| 75 | > "$W/omega.out" 2>&1 < /dev/null & |
| 76 | SPID=$! |
| 77 | up=0 |
| 78 | for i in 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; do |
| 79 | if curl -s -o /dev/null --max-time 1 "http://127.0.0.1:$OP/health" && |
| 80 | curl -s -o /dev/null --max-time 1 "http://127.0.0.1:$SP/health"; then up=1; break; fi |
| 81 | sleep 1 |
| 82 | done |
| 83 | [ $up = 1 ] || fail "servers did not come up" |
| 84 | SINCE=$(date -u +%Y-%m-%dT%H:%M:%S) |
| 85 | "$W/pdrive" "$W/corpus.tsv" $OP "$W/oracle.rec" || fail "driving the oracle failed" |
| 86 | "$W/pdrive" "$W/corpus.tsv" $SP "$W/omega.rec" || fail "driving Omega failed" |
| 87 | stop |
| 88 | "$W/pcmp" "$W/corpus.tsv" "$W/oracle.rec" "$W/omega.rec" "$W/report.txt" "$SINCE" |
| 89 | rc=$? |
| 90 | [ $rc -le 1 ] || fail "comparator error rc=$rc" |
| 91 | awk '$1=="PASS"||$1=="FAIL" { n[$3]++; if ($1=="PASS") p[$3]++ } |
| 92 | END { for (c in n) printf "%-24s %5d / %5d\n", c, p[c]+0, n[c] }' "$W/report.txt" | sort > "$W/classes.txt" |
| 93 | { |
| 94 | echo "parity gate $(date -u +%Y-%m-%dT%H:%M:%SZ) rc=$rc" |
| 95 | echo "corpus $(wc -l < "$W/corpus.tsv") requests, sha256 $(sha256sum < "$W/corpus.tsv" | cut -c1-64)" |
| 96 | echo "generator $(cat "$W/gen.out")" |
| 97 | echo "source $(sqlite3 "file:$SRC?mode=ro" 'select count(*) from entities') entities (copy of a cortex-rs database)" |
| 98 | echo "import $(cat "$W/import.out")" |
| 99 | echo "omc sha256 $(sha256sum < "$OMC" | cut -c1-64)" |
| 100 | echo "omega sha256 $(sha256sum < "$BIN" | cut -c1-64)" |
| 101 | echo "oracle sha256 $(sha256sum < "$ORA" | cut -c1-64)" |
| 102 | echo "since $SINCE" |
| 103 | tail -1 "$W/report.txt" |
| 104 | echo "per class (pass / total):" |
| 105 | cat "$W/classes.txt" |
| 106 | grep '^FAIL' "$W/report.txt" | head -50 |
| 107 | } > "$EV/summary.txt" |
| 108 | cp "$W/report.txt" "$EV/report.txt" |
| 109 | cat "$EV/summary.txt" |
| 110 | exit $rc |