omega-harness
Agent skills for operating, encoding and communicating in Omega (.om) and the AIEN repositories
Brought by drakestapleton
omega-harness / examples / gate.sh
| 1 | #!/usr/bin/env bash |
| 2 | # gate.sh OMC: compile and run every worked example against its expected result. |
| 3 | # |
| 4 | # Covers examples/*.om and skills/*/examples/*.om. Each NAME.om needs NAME.expect: |
| 5 | # compile N required; the compiler's exit status |
| 6 | # stderr LINE required when N is not 0; the compiler's whole stderr is exactly LINE |
| 7 | # exit N required when N is 0; the program's exit status |
| 8 | # input dir optional; the compiler reads a directory instead of NAME.om (error 1) |
| 9 | # output full optional; the compiler writes to /dev/full (error 2) |
| 10 | # Optional fixtures: NAME.stdout (exact stdout bytes, absent means empty), NAME.stdin (piped in, |
| 11 | # so reads can be partial), NAME.args (one argument per line), NAME.env (one KEY=VALUE per line). |
| 12 | # Every program runs with `env -i` plus NAME.env, in an empty scratch directory, under a 20 s |
| 13 | # timeout, and must write nothing to stderr. A compile that is not expected to succeed is never run. |
| 14 | # |
| 15 | # The compiler must match the omc sha256 in the repository's PIN file (the one place the pin is |
| 16 | # written); a different binary is refused, not tolerated. |
| 17 | # Exit 0 only when every example passes; 1 on any failure; 2 on a usage or pin error. |
| 18 | set -u |
| 19 | |
| 20 | here=$(cd "$(dirname "$0")" && pwd) |
| 21 | root=$(dirname "$here") |
| 22 | PIN_REV=$(sed -n 's/^omega-mv //p' "$root/PIN" 2>/dev/null) |
| 23 | PIN_SHA=$(sed -n 's/^omc-sha256 //p' "$root/PIN" 2>/dev/null) |
| 24 | if [ -z "$PIN_REV" ] || [ -z "$PIN_SHA" ]; then |
| 25 | echo "GATE FAIL $root/PIN must hold 'omega-mv <commit>' and 'omc-sha256 <sha256>'" >&2 |
| 26 | exit 2 |
| 27 | fi |
| 28 | |
| 29 | if [ "$#" -ne 1 ]; then |
| 30 | echo "usage: $0 /path/to/omc (omega-mv $PIN_REV seed/out/omc)" >&2 |
| 31 | exit 2 |
| 32 | fi |
| 33 | omc=$(cd "$(dirname "$1")" && pwd)/$(basename "$1") |
| 34 | if [ ! -x "$omc" ]; then |
| 35 | echo "GATE FAIL $1 is not an executable file" >&2 |
| 36 | exit 2 |
| 37 | fi |
| 38 | got=$(sha256sum "$omc" | cut -d' ' -f1) |
| 39 | if [ "$got" != "$PIN_SHA" ]; then |
| 40 | echo "GATE FAIL omc sha256 $got, pinned $PIN_SHA (omega-mv $PIN_REV)" >&2 |
| 41 | exit 2 |
| 42 | fi |
| 43 | |
| 44 | work=$(mktemp -d) |
| 45 | trap 'rm -rf "$work"' EXIT |
| 46 | |
| 47 | pass=0 |
| 48 | fail=0 |
| 49 | total=0 |
| 50 | |
| 51 | bad() { |
| 52 | echo "FAIL $name: $*" |
| 53 | fail=$((fail + 1)) |
| 54 | } |
| 55 | |
| 56 | # Expected files without a program are a silent loss of coverage, so they fail too. |
| 57 | for f in "$root"/examples/*.expect "$root"/skills/*/examples/*.expect; do |
| 58 | [ -e "$f" ] || continue |
| 59 | if [ ! -e "${f%.expect}.om" ]; then |
| 60 | name=$(basename "${f%.expect}") |
| 61 | total=$((total + 1)) |
| 62 | bad "expect file without $name.om" |
| 63 | fi |
| 64 | done |
| 65 | |
| 66 | for src in "$root"/examples/*.om "$root"/skills/*/examples/*.om; do |
| 67 | [ -e "$src" ] || continue |
| 68 | base=${src%.om} |
| 69 | name=${base#"$root"/} |
| 70 | total=$((total + 1)) |
| 71 | if [ ! -f "$base.expect" ]; then |
| 72 | bad "no $name.expect" |
| 73 | continue |
| 74 | fi |
| 75 | want_compile= want_stderr= want_exit= input=file output=file |
| 76 | while IFS= read -r line || [ -n "$line" ]; do |
| 77 | case $line in |
| 78 | "compile "*) want_compile=${line#compile } ;; |
| 79 | "stderr "*) want_stderr=${line#stderr } ;; |
| 80 | "exit "*) want_exit=${line#exit } ;; |
| 81 | "input dir") input=dir ;; |
| 82 | "output full") output=full ;; |
| 83 | "") ;; |
| 84 | *) want_compile=invalid ;; |
| 85 | esac |
| 86 | done < "$base.expect" |
| 87 | case $want_compile in |
| 88 | ''|invalid|*[!0-9]*) bad "malformed $name.expect"; continue ;; |
| 89 | esac |
| 90 | if [ "$want_compile" != 0 ] && [ -z "$want_stderr" ]; then |
| 91 | bad "$name.expect has no stderr line"; continue |
| 92 | fi |
| 93 | if [ "$want_compile" = 0 ]; then |
| 94 | case $want_exit in |
| 95 | ''|*[!0-9]*) bad "$name.expect has no exit line"; continue ;; |
| 96 | esac |
| 97 | fi |
| 98 | |
| 99 | d=$work/$(echo "$name" | tr / _) |
| 100 | mkdir -p "$d/run" |
| 101 | in=$src |
| 102 | [ "$input" = dir ] && in=$d/run |
| 103 | out=$d/p |
| 104 | [ "$output" = full ] && out=/dev/full |
| 105 | "$omc" < "$in" > "$out" 2> "$d/cerr" |
| 106 | rc=$? |
| 107 | if [ "$rc" != "$want_compile" ]; then |
| 108 | bad "compile exit $rc, expected $want_compile: $(cat "$d/cerr")" |
| 109 | continue |
| 110 | fi |
| 111 | if [ "$want_compile" != 0 ]; then |
| 112 | if printf '%s\n' "$want_stderr" | cmp -s - "$d/cerr"; then |
| 113 | echo "PASS $name (compile $rc: $want_stderr)" |
| 114 | pass=$((pass + 1)) |
| 115 | else |
| 116 | bad "compiler stderr '$(cat "$d/cerr")', expected '$want_stderr'" |
| 117 | fi |
| 118 | continue |
| 119 | fi |
| 120 | if [ -s "$d/cerr" ]; then |
| 121 | bad "compiler stderr not empty: $(cat "$d/cerr")" |
| 122 | continue |
| 123 | fi |
| 124 | |
| 125 | chmod +x "$d/p" |
| 126 | args=() |
| 127 | envs=() |
| 128 | if [ -f "$base.args" ]; then mapfile -t args < "$base.args"; fi |
| 129 | if [ -f "$base.env" ]; then mapfile -t envs < "$base.env"; fi |
| 130 | if [ -f "$base.stdin" ]; then |
| 131 | (cd "$d/run" && cat "$base.stdin" | timeout 20 env -i "${envs[@]}" "$d/p" "${args[@]}" > "$d/out" 2> "$d/rerr") |
| 132 | else |
| 133 | (cd "$d/run" && timeout 20 env -i "${envs[@]}" "$d/p" "${args[@]}" < /dev/null > "$d/out" 2> "$d/rerr") |
| 134 | fi |
| 135 | rc=$? |
| 136 | want_out=/dev/null |
| 137 | [ -f "$base.stdout" ] && want_out=$base.stdout |
| 138 | if [ "$rc" != "$want_exit" ]; then |
| 139 | bad "exit $rc, expected $want_exit" |
| 140 | elif ! cmp -s "$want_out" "$d/out"; then |
| 141 | bad "stdout differs from $(basename "$want_out"): $(od -An -c "$d/out" | tr -s ' ' | tr -d '\n' | cut -c1-200)" |
| 142 | elif [ -s "$d/rerr" ]; then |
| 143 | bad "program stderr not empty: $(cat "$d/rerr")" |
| 144 | else |
| 145 | echo "PASS $name (exit $rc, $(wc -c < "$d/out" | tr -d ' ') stdout bytes)" |
| 146 | pass=$((pass + 1)) |
| 147 | fi |
| 148 | done |
| 149 | |
| 150 | if [ "$total" = 0 ]; then |
| 151 | echo "GATE FAIL no examples found under $root" |
| 152 | exit 1 |
| 153 | fi |
| 154 | if [ "$fail" = 0 ] && [ "$pass" = "$total" ]; then |
| 155 | echo "GATE PASS examples=$total pass=$pass fail=0 omc=$PIN_SHA" |
| 156 | exit 0 |
| 157 | fi |
| 158 | echo "GATE FAIL examples=$total pass=$pass fail=$fail omc=$got" |
| 159 | exit 1 |