interplane.Get Omega
Read in your language

cortex

aien-dev/cortex

Brought by drakestapleton

branch main Branches Tags History

cortex / tests / brain / mutant.sh

70 lines · 4305 bytes Raw Blame History
1#!/bin/sh
2# tests/brain/mutant.sh : negative controls for the Company Brain gate (tests/brain/run.sh).
3# sh tests/brain/mutant.sh
4# Each mutant (thirteen, listed below) changes exactly one line of build/cortex.unit.om inside one function
5# of om/brain.om (or, for the last three, of the incremental FTS index in om/fts.om),
6# is compiled with the pinned omc and run through the whole brain gate on its own port and scratch
7# directories. Every mutant must be caught: gate rc 1 with at least one FAIL line. Writes
8# $BRAIN_EVIDENCE/mutants.txt (default build/brain). Exit 0 only when every mutant is caught,
9# 1 when one survives, 2 on a setup error.
10set -u
11R=$(cd "$(dirname "$0")/../.." && pwd)
12M=${BRAIN_MUTANT_WORK:-$R/build/brain-mutant}
13EV=${BRAIN_EVIDENCE:-$R/build/brain}
14PORT=${BRAIN_MUTANT_PORT:-18233}
15mkdir -p "$EV"
16[ -s "$R/build/cortex.unit.om" ] || { echo "brain mutant: build first (sh build.sh)" >&2; exit 2; }
17OMC=$(sh "$R/tools/omc.sh") || exit 2
18rm -rf "$M" && mkdir -p "$M"
19out=$EV/mutants.txt
20echo "brain mutants $(date -u +%Y-%m-%dT%H:%M:%SZ), base unit sha256 $(sha256sum < "$R/build/cortex.unit.om" | cut -c1-64)" > "$out"
21survived=0
22errors=0
23# NAME|FUNCTION|LINE|REPLACEMENT|PROPERTY
24while IFS='|' read -r name fn from to what; do
25 [ -n "$name" ] || continue
26 d=$M/$name
27 mkdir -p "$d"
28 awk -v fn="fn $fn " -v from="$from" -v to="$to" '
29 index($0, "fn ") == 1 { f = (index($0, fn) == 1) }
30 f && n == 0 && $0 == from { print to; n = 1; next }
31 { print }
32 END { if (n != 1) exit 3 }' "$R/build/cortex.unit.om" > "$d/cortex.unit.om" ||
33 { echo "brain mutant $name: site not found" >&2; exit 2; }
34 chg=$(diff "$R/build/cortex.unit.om" "$d/cortex.unit.om" | grep -c '^[<>]')
35 [ "$chg" -eq 2 ] || { echo "brain mutant $name: expected one changed line, got $chg diff lines" >&2; exit 2; }
36 "$OMC" < "$d/cortex.unit.om" > "$d/cortex" 2> "$d/cortex.err"
37 if [ $? -ne 0 ] || [ -s "$d/cortex.err" ]; then echo "brain mutant $name: compile failed: $(cat "$d/cortex.err")" >&2; exit 2; fi
38 chmod +x "$d/cortex"
39 CORTEX_BIN=$d/cortex BRAIN_PORT=$PORT BRAIN_WORK=$d/work BRAIN_EVIDENCE=$d/ev sh "$R/tests/brain/run.sh" > "$d/run.log" 2>&1
40 rc=$?
41 nf=$(grep -c '^FAIL' "$d/run.log")
42 first=$(grep '^FAIL' "$d/run.log" | head -2 | cut -c1-160 | tr '\n' ';')
43 if [ $rc -eq 1 ] && [ "$nf" -gt 0 ]; then
44 echo "CAUGHT $name ($what): fn $fn, '$from' -> '$to'; gate rc $rc, $nf FAIL line(s): $first" >> "$out"
45 elif [ $rc -eq 2 ]; then
46 echo "ERROR $name: the gate could not run (rc 2): $(grep '^brain:' "$d/run.log" | head -1)" >> "$out"
47 errors=$((errors + 1))
48 else
49 echo "SURVIVED $name ($what): fn $fn, '$from' -> '$to'; gate rc $rc" >> "$out"
50 survived=$((survived + 1))
51 fi
52done <<'EOF'
53supersede|bn_live|ret eq get bsup e 0|ret true|a superseded entry is never returned by read-before
54cite-drop|bn_ocite|for i ulong n|for i ulong div n 2|every returned entry is cited
55cite-id|bn_ocite|o_hs get enm e|o_hs get evf e|citation ids resolve to stored entries
56confirm-gate|bn_aok|if not bn_conf get ect e|if false|an unconfirmed contract never appears in alerts
57date-kinds|bn_okind|js_raw bo_k2|js_raw bo_k1|on_completion and hard_expiry stay distinct
58import-idem|bn_import|if bn_same e k b|if false|an unchanged import changes nothing
59window-end|bn_inwin|ret le ulong sub y dd get bfa 3|ret lt ulong sub y dd get bfa 3|day N is inside the window
60window-start|bn_inwin|if lt y dd|if le y dd|day 0 (the date itself) is inside the window
61guard|bn_guard|ret x|ret false|the generic write may not revise a Brain entry
62index-update|bn_commit|put bsup t add e 1|t = t|each Brain write updates the live supersede index
63fts-count|fts_lstep|put qdf t uint add ulong get qdf t 1|put qdf t uint ulong get qdf t|each put counts its terms in the FTS row counts
64fts-link|fts_lstep|put qlh t uint add k 1|put qlv 1 get qlv 1|each put links its terms into the FTS term lists
65fts-space|fts_gadd|put qgc g uint add ulong get qgc g 1|put qgc g get qgc g|each put counts its row in its space's list
66EOF
67cat "$out"
68if [ $errors -gt 0 ]; then echo "brain mutants: $errors could not be run" | tee -a "$out"; exit 2; fi
69if [ $survived -eq 0 ]; then echo "brain mutants: ALL CAUGHT" | tee -a "$out"; exit 0; fi
70echo "brain mutants: $survived survived" | tee -a "$out"; exit 1