interplane.Get Omega
Read in your language

cortex

aien-dev/cortex

Brought by drakestapleton

branch main Branches Tags History

cortex / tests / shadow / run.sh

118 lines · 6055 bytes Raw Blame History
1#!/bin/sh
2# tests/shadow/run.sh : shadow comparison of a running cortex-rs against the Omega server, on the
3# same data, with a corpus of real read requests.
4# CORTEX_TOKEN=... sh tests/shadow/run.sh LIVE_DB LIVE_PORT CORPUS OUTDIR
5# LIVE_DB is the database the running cortex-rs on 127.0.0.1:LIVE_PORT serves (opened read-only;
6# the running server is never stopped or reconfigured). CORPUS is in the parity format
7# (spec/PARITY.md), read requests only; flag a sends "authorization: Bearer $CORTEX_TOKEN".
8# Each attempt:
9# 1. fingerprints the live data (every entity id/revision/retracted/name/content length and every
10# claim id/retracted), takes a read-only .backup, fingerprints again (retry if they differ);
11# 2. imports the snapshot and starts build/cortex on SHADOW_PORT (default 18280) with the same
12# encoder URL (SHADOW_ENC, default http://127.0.0.1:18081);
13# 3. replays CORPUS to the live server, fingerprints the live data again: if anything was written
14# between the snapshot and the end of the live replay, the attempt is discarded (drift), since
15# both servers must see the same rows; otherwise replays CORPUS to the Omega server and compares
16# the answers with pcmp (timing fields masked as in the parity gate).
17# Up to SHADOW_TRIES (default 20) attempts; the first drift-free attempt is the result.
18# OUTDIR gets summary.txt, report.txt, live.rec, omega.rec, attempts.txt. Exit 0 only when a
19# drift-free attempt compared with zero failures.
20set -u
21DB=${1:?usage: run.sh LIVE_DB LIVE_PORT CORPUS OUTDIR}
22LP=${2:?usage: run.sh LIVE_DB LIVE_PORT CORPUS OUTDIR}
23CORPUS=${3:?usage: run.sh LIVE_DB LIVE_PORT CORPUS OUTDIR}
24OUT=${4:?usage: run.sh LIVE_DB LIVE_PORT CORPUS OUTDIR}
25R=$(cd "$(dirname "$0")/../.." && pwd)
26P=$R/tests/parity
27BIN=${CORTEX_BIN:-$R/build/cortex}
28IMP=${CORTEX_IMPORT:-$R/build/cortex-import}
29SP=${SHADOW_PORT:-18280}
30ENC=${SHADOW_ENC:-http://127.0.0.1:18081}
31TRIES=${SHADOW_TRIES:-20}
32W=${SHADOW_WORK:-$R/build/shadow}
33SPID=
34# stop the Omega server and wait (up to 5 s) until its acceptor has released the port
35stop() {
36 [ -n "$SPID" ] && kill "$SPID" 2>/dev/null; SPID=
37 i=0
38 while [ $i -lt 50 ] && curl -s -o /dev/null --max-time 1 "http://127.0.0.1:$SP/health"; do sleep 0.1; i=$((i + 1)); done
39}
40fail() { echo "shadow: $*" >&2; stop; exit 2; }
41[ -n "${CORTEX_TOKEN:-}" ] || fail "CORTEX_TOKEN is not set"
42[ -f "$DB" ] && [ -f "$CORPUS" ] || fail "missing LIVE_DB or CORPUS"
43[ -x "$BIN" ] && [ -x "$IMP" ] || fail "build first (sh build.sh)"
44curl -s -o /dev/null --max-time 1 "http://127.0.0.1:$SP/health" && fail "port $SP is in use"
45curl -sf -o /dev/null --max-time 2 "http://127.0.0.1:$LP/health" || fail "live server on $LP not answering"
46OMC=$(sh "$R/tools/omc.sh") || fail "pinned omc unavailable"
47rm -rf "$W"
48mkdir -p "$W" "$OUT" || fail "cannot make $W"
49for u in pdrive pcmp; do
50 { echo om2; cat "$P/$u.om"; } > "$W/$u.om"
51 "$OMC" < "$W/$u.om" > "$W/$u" 2> "$W/$u.err"
52 if [ $? -ne 0 ] || [ -s "$W/$u.err" ]; then fail "$u.om did not compile: $(cat "$W/$u.err")"; fi
53 chmod +x "$W/$u"
54done
55fp() {
56 sqlite3 -readonly -batch "file:$DB?mode=ro" \
57 "select id, revision, retracted, canonical_name, length(content) from entities order by id;
58 select id, retracted from claims order by id;" | sha256sum | cut -c1-64
59}
60: > "$OUT/attempts.txt"
61ok=0
62a=0
63while [ $a -lt "$TRIES" ]; do
64 a=$((a + 1))
65 stop
66 rm -f "$W/s.db" "$W/cx.log" "$W/cx.log.sock"
67 f0=$(fp)
68 sqlite3 "file:$DB?mode=ro" ".backup '$W/s.db'" || fail "backup failed"
69 f1=$(fp)
70 if [ "$f0" != "$f1" ]; then echo "attempt $a: write during backup" >> "$OUT/attempts.txt"; sleep 5; continue; fi
71 "$IMP" "$W/s.db" "$W/cx.log" > "$W/import.out" 2>&1 || fail "import failed: $(cat "$W/import.out")"
72 setsid nohup "$BIN" --port "$SP" --log "$W/cx.log" --encoder-url "$ENC" > "$W/omega.out" 2>&1 < /dev/null &
73 SPID=$!
74 up=0
75 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
76 curl -sf -o /dev/null --max-time 1 "http://127.0.0.1:$SP/health" && { up=1; break; }
77 sleep 1
78 done
79 [ $up = 1 ] || fail "Omega server did not come up: $(cat "$W/omega.out")"
80 SINCE=$(date -u +%Y-%m-%dT%H:%M:%S)
81 t0=$(date +%s)
82 PDRIVE_TOKEN=$CORTEX_TOKEN "$W/pdrive" "$CORPUS" "$LP" "$W/live.rec" || fail "driving the live server failed"
83 f2=$(fp)
84 t1=$(date +%s)
85 if [ "$f0" != "$f2" ]; then
86 echo "attempt $a: live data changed during the live replay (${t0}..${t1})" >> "$OUT/attempts.txt"
87 continue
88 fi
89 PDRIVE_TOKEN=$CORTEX_TOKEN "$W/pdrive" "$CORPUS" "$SP" "$W/omega.rec" || fail "driving Omega failed"
90 stop
91 echo "attempt $a: drift-free, live replay ${t0}..${t1}" >> "$OUT/attempts.txt"
92 ok=1
93 break
94done
95stop
96[ $ok = 1 ] || { cat "$OUT/attempts.txt"; fail "no drift-free attempt in $TRIES tries"; }
97"$W/pcmp" "$CORPUS" "$W/live.rec" "$W/omega.rec" "$W/report.txt" "$SINCE"
98rc=$?
99[ $rc -le 1 ] || fail "comparator error rc=$rc"
100errs=$(grep -c ' ERR$' "$W/live.rec" "$W/omega.rec" | awk -F: '{ s += $2 } END { print s + 0 }')
101{
102 echo "shadow $(date -u +%Y-%m-%dT%H:%M:%SZ) rc=$rc"
103 echo "corpus $(wc -l < "$CORPUS") requests, sha256 $(sha256sum < "$CORPUS" | cut -c1-64)"
104 echo "snapshot $(sqlite3 "file:$W/s.db?mode=ro" 'select count(*) from entities') entities, $(sqlite3 "file:$W/s.db?mode=ro" 'select count(*) from claims') claims, fingerprint $f0"
105 echo "import $(cat "$W/import.out")"
106 echo "omega sha256 $(sha256sum < "$BIN" | cut -c1-64)"
107 echo "omc sha256 $(sha256sum < "$OMC" | cut -c1-64)"
108 echo "attempts $a ($(tail -1 "$OUT/attempts.txt"))"
109 echo "transport errors (ERR records, both sides) $errs"
110 tail -1 "$W/report.txt"
111 awk '$1=="PASS"||$1=="FAIL" { split($3, c, "."); k = c[1] "." c[2]; n[k]++; if ($1=="PASS") p[k]++ }
112 END { for (k in n) printf "%-24s %5d / %5d\n", k, p[k]+0, n[k] }' "$W/report.txt" | sort
113 grep '^FAIL' "$W/report.txt" | head -50
114} > "$OUT/summary.txt"
115cp "$W/report.txt" "$W/live.rec" "$W/omega.rec" "$OUT/"
116cat "$OUT/summary.txt"
117[ "$errs" = 0 ] || rc=1
118exit $rc