interplane.Get Omega
Read in your language

cortex

aien-dev/cortex

Brought by drakestapleton

branch main Branches Tags History

cortex / tests / parity / run.sh

110 lines · 5306 bytes Raw Blame History
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).
16set -u
17SRC=${1:?usage: run.sh SRC.db}
18R=$(cd "$(dirname "$0")/../.." && pwd)
19P=$R/tests/parity
20W=${PARITY_WORK:-$R/build/parity}
21EV=${PARITY_EVIDENCE:-$R/evidence/parity}
22BIN=${CORTEX_BIN:-$R/build/cortex}
23IMP=${CORTEX_IMPORT:-$R/build/cortex-import}
24ORA=${CORTEX_ORACLE:-$(command -v cortex-rs)}
25OP=${PARITY_ORACLE_PORT:-18180}
26SP=${PARITY_OMEGA_PORT:-18190}
27TOK=paritytok
28ENC=http://127.0.0.1:9
29fail() { echo "parity: $*" >&2; stop; exit 2; }
30OPID=
31SPID=
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.
34freeport() {
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}
41stop() {
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)"
51for port in $OP $SP; do
52 freeport $port || fail "port $port is in use"
53done
54OMC=$(sh "$R/tools/omc.sh") || fail "pinned omc unavailable"
55rm -rf "$W"
56mkdir -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)
59SOCKD=$(mktemp -d /tmp/cxsock.XXXXXX) || fail "cannot make a socket directory"
60trap 'rm -rf "$SOCKD"' EXIT
61for 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"
66done
67sh "${PARITY_GEN:-$P/gen.sh}" "$SRC" "$W/corpus.tsv" > "$W/gen.out" || fail "corpus generation failed"
68sqlite3 "file:$SRC?mode=ro" ".backup '$W/o.db'" || fail "copy failed"
69sqlite3 "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")"
71CORTEX_TOKEN=$TOK setsid nohup "$ORA" --port $OP --db-path "$W/o.db" --encoder-url $ENC \
72 > "$W/oracle.out" 2>&1 < /dev/null &
73OPID=$!
74CORTEX_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 &
76SPID=$!
77up=0
78for 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
82done
83[ $up = 1 ] || fail "servers did not come up"
84SINCE=$(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"
87stop
88"$W/pcmp" "$W/corpus.tsv" "$W/oracle.rec" "$W/omega.rec" "$W/report.txt" "$SINCE"
89rc=$?
90[ $rc -le 1 ] || fail "comparator error rc=$rc"
91awk '$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"
108cp "$W/report.txt" "$EV/report.txt"
109cat "$EV/summary.txt"
110exit $rc