Interplane page as plain text, for reading tools and agents. English is the reference text: translate it with your own tool. Text in backticks and in ``` blocks (commands, fingerprints, file names) and link targets in <...> are exact: copy them, never translate them. Page: /drakestapleton/cortex/blob/main/tests/brain/run.sh # cortex aien-dev/cortex Brought by drakestapleton Sign in to star 0 stars 0 watching About Files History Releases Proposed changes Issues For developers branch `main` Branches Tags History `cortex / tests / brain / run.sh` 122 lines ยท 7136 bytes Raw Blame History 1 #!/bin/sh 2 # tests/brain/run.sh : Company Brain gate (om/brain.om; spec/CORTEX-API.md "Company Brain"). 3 # sh tests/brain/run.sh 4 # Starts the server on an empty log (port $BRAIN_PORT, default 18232) and runs three phases with the 5 # Omega parity driver and comparator (tests/parity/pdrive.om, pcmp.om): 6 # 1. tests/brain/corpus1.tsv: write after, supersede, read before with citations, refusals, the 7 # append-only guard on the generic write, contract and party records, the confirmation gate and 8 # the alert window boundaries; 9 # 2. export the Brain (cortex --brain-export) and import every exported file unchanged 10 # (tests/brain/mkimp.om builds the requests): nothing may change, and a second export must have 11 # the same tree hash; 12 # 3. hand-correct two exported files (confirm amendment a1, change decision d2), import them, then 13 # tests/brain/corpus3.tsv (alerts with three date kinds, read before after the correction). 14 # Each phase's answers must equal the committed expected-.rec under the parity rule 15 # (spec/PARITY.md), and each export's tree hash must equal the one in tests/brain/expected.sha. 16 # Tree hash: sha256 of the `sha256sum` lines of every file, paths relative to the export directory, 17 # in C-locale byte order. Writes $BRAIN_EVIDENCE/summary.txt (default build/brain). 18 # BRAIN_RECORD=1 writes the expected files (expected-*.rec, expected.sha) instead of comparing: that is 19 # the only way they are made; they are never edited by hand. Review `git diff tests/brain` before 20 # committing a re-record. 21 # Exit 0 only when every phase passes; 1 on a failure; 2 on a setup error. 22 set -u 23 R=$(cd "$(dirname "$0")/../.." && pwd) 24 T=$R/tests/brain 25 P=$R/tests/parity 26 W=${BRAIN_WORK:-$R/build/brain-work} 27 EV=${BRAIN_EVIDENCE:-$R/build/brain} 28 BIN=${CORTEX_BIN:-$R/build/cortex} 29 PORT=${BRAIN_PORT:-18232} 30 TOK=paritytok 31 SPID= 32 fail() { echo "brain: $*" >&2; stop; exit 2; } 33 stop() { # end the server's whole process group (setsid) and wait until the port answers no more 34 [ -n "$SPID" ] || return 0 35 kill -- "-$SPID" 2>/dev/null || kill "$SPID" 2>/dev/null 36 SPID= 37 for i in 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20; do 38 curl -s -o /dev/null --max-time 1 "http://127.0.0.1:$PORT/health" || return 0 39 sleep 0.5 40 done 41 } 42 [ -x "$BIN" ] || fail "build first (sh build.sh)" 43 if curl -s -o /dev/null --max-time 1 "http://127.0.0.1:$PORT/health"; then fail "port $PORT is in use"; fi 44 OMC=$(sh "$R/tools/omc.sh") || fail "pinned omc unavailable" 45 rm -rf "$W" 46 mkdir -p "$W" "$EV" || fail "cannot make $W" 47 # the server's private socket defaults to LOG.sock; a sockaddr_un path holds at most 108 bytes (a 48 # kernel boundary), so the work directory may be too deep for it: use a short directory instead 49 SOCKD=$(mktemp -d /tmp/cxbrain.XXXXXX) || fail "cannot make a socket directory" 50 trap 'rm -rf "$SOCKD"' EXIT 51 for u in "$P/pdrive" "$P/pcmp" "$T/mkimp"; do 52 n=$(basename "$u") 53 { echo om2; cat "$u.om"; } > "$W/$n.om" 54 "$OMC" < "$W/$n.om" > "$W/$n" 2> "$W/$n.err" 55 if [ $? -ne 0 ] || [ -s "$W/$n.err" ]; then fail "$n.om did not compile: $(cat "$W/$n.err")"; fi 56 chmod +x "$W/$n" 57 done 58 CORTEX_TOKEN=$TOK setsid nohup "$BIN" --port "$PORT" --log "$W/cx.log" --sock "$SOCKD/s" --encoder-url http://127.0.0.1:9 \ 59 > "$W/server.out" 2>&1 < /dev/null & 60 SPID=$! 61 up=0 62 for i in 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20; do 63 if curl -s -o /dev/null --max-time 1 "http://127.0.0.1:$PORT/health"; then up=1; break; fi 64 sleep 1 65 done 66 [ $up = 1 ] || fail "server did not come up" 67 SINCE=$(date -u +%Y-%m-%dT%H:%M:%S) 68 fails=0 69 : > "$EV/summary.txt" 70 say() { echo "$*"; echo "$*" >> "$EV/summary.txt"; } 71 treehash() { (cd "$1" && find . -type f | LC_ALL=C sort | xargs sha256sum) | sha256sum | cut -c1-64; } 72 phase() { # phase N CORPUS 73 "$W/pdrive" "$2" "$PORT" "$W/p$1.rec" || fail "driving phase $1 failed" 74 if [ "${BRAIN_RECORD:-0}" = 1 ]; then cp "$W/p$1.rec" "$T/expected-$1.rec"; say "RECORDED phase $1"; return; fi 75 "$W/pcmp" "$2" "$T/expected-$1.rec" "$W/p$1.rec" "$EV/report-$1.txt" "$SINCE" > /dev/null 2>&1 76 rc=$? 77 if [ $rc -eq 0 ]; then say "PASS phase $1 ($(grep -c '^PASS' "$EV/report-$1.txt") records)" 78 else say "FAIL phase $1 rc=$rc: $(grep '^FAIL' "$EV/report-$1.txt" | head -5 | tr '\n' ';')"; fails=$((fails + 1)); fi 79 } 80 export_check() { # export_check NAME 81 if ! "$BIN" --log "$W/cx.log" --brain-export "$W/$1" > "$W/$1.out" 2>&1; then 82 say "FAIL export $1: $(cat "$W/$1.out")"; stop; say "brain: export failed, later phases NOT_RUN"; exit 1 83 fi 84 h=$(treehash "$W/$1") 85 if [ "${BRAIN_RECORD:-0}" = 1 ]; then echo "$1 $h" >> "$W/expected.sha"; say "RECORDED $1 $h"; return; fi 86 want=$(awk -v n="$1" '$1 == n { print $2 }' "$T/expected.sha") 87 if [ "$h" = "$want" ]; then say "PASS export $1 tree sha256 $h ($(cat "$W/$1.out"))" 88 else say "FAIL export $1 tree sha256 $h, expected $want"; fails=$((fails + 1)); fi 89 } 90 phase 1 "$T/corpus1.tsv" 91 export_check x1 92 (cd "$W" && "$W/mkimp" bgate drake 2026-11-12 $(find x1 -type f | LC_ALL=C sort)) > "$W/corpus2.tsv" 2> "$W/mkimp.err" || 93 fail "mkimp failed: $(cat "$W/mkimp.err")" 94 phase 2 "$W/corpus2.tsv" 95 export_check x2 96 if [ "$(treehash "$W/x1")" = "$(treehash "$W/x2")" ]; then say "PASS idempotent: export, unchanged import, export give one tree hash" 97 else say "FAIL idempotent: the unchanged import changed the export"; fails=$((fails + 1)); fi 98 mkdir -p "$W/edit" 99 sed 's/^confirmed: no$/confirmed: yes/' "$W/x1/bgate/contract/2026-11-10/a1.txt" > "$W/edit/a1.txt" 100 sed 's/loading of 15 percent/loading of 16 percent/' "$W/x1/bgate/decision/2026-04-01/d2.txt" > "$W/edit/d2.txt" 101 cmp -s "$W/edit/a1.txt" "$W/x1/bgate/contract/2026-11-10/a1.txt" && fail "the a1 hand edit changed nothing" 102 cmp -s "$W/edit/d2.txt" "$W/x1/bgate/decision/2026-04-01/d2.txt" && fail "the d2 hand edit changed nothing" 103 (cd "$W" && "$W/mkimp" bgate drake 2026-11-12 edit/a1.txt edit/d2.txt) > "$W/corpus3.tsv" || fail "mkimp failed" 104 cat "$T/corpus3.tsv" >> "$W/corpus3.tsv" 105 phase 3 "$W/corpus3.tsv" 106 # the live supersede index (updated by each write since the server started) must equal a full rebuild 107 printf 'brain.usage\tPOST\t/api/cortex/brain/usage\taj\t{}\n' > "$W/usage.tsv" 108 "$W/pdrive" "$W/usage.tsv" "$PORT" "$W/usage.out" || fail "usage request failed" 109 mm=$(grep -a -o '"supersedeIndexMismatches":[a-z0-9]*' "$W/usage.out") 110 if [ "$mm" = '"supersedeIndexMismatches":0' ]; then say "PASS live supersede index equals a full rebuild ($mm)" 111 else say "FAIL live supersede index differs from a full rebuild ($mm)"; fails=$((fails + 1)); fi 112 # the incremental FTS index (term lists, row counts, space lists; om/fts.om) must equal a rebuild 113 fm=$(grep -a -o '"ftsIndexMismatches":[a-z0-9]*' "$W/usage.out") 114 if [ "$fm" = '"ftsIndexMismatches":0' ]; then say "PASS incremental FTS index equals a rebuild ($fm)" 115 else say "FAIL incremental FTS index differs from a rebuild ($fm)"; fails=$((fails + 1)); fi 116 export_check x3 117 stop 118 rm -rf "$SOCKD" 119 [ "${BRAIN_RECORD:-0}" = 1 ] && cp "$W/expected.sha" "$T/expected.sha" 120 say "server $(sha256sum "$BIN" | cut -c1-64)" 121 if [ $fails -eq 0 ]; then say "brain: ALL PASS"; exit 0; fi 122 say "brain: $fails failure(s)"; exit 1