aien-architecture-mv
Canonical architecture for the AIEN ecosystem: flows, boundaries, ADRs, and the map from design to implementation repositories.
Brought by drakestapleton
aien-architecture-mv / scripts / test_check_candidate.sh
| 1 | #!/usr/bin/env bash |
| 2 | # Test for tools/candidate/check_candidate.om. Offline: sha existence is stubbed (CAND_SHA_CHECK) and, |
| 3 | # for the real gh code path, a fake `gh` on PATH. No network or auth needed. The checker is compiled |
| 4 | # here with the pinned seeded compiler named by AIEN_OMC (exit 3 = NOT_RUN without it); the mutants |
| 5 | # below are sed edits of the .om source, recompiled, each proving one check bites. |
| 6 | set -u |
| 7 | here="$(cd "$(dirname "$0")" && pwd)" |
| 8 | src="$here/../tools/candidate/check_candidate.om" |
| 9 | omc="${AIEN_OMC:-}" |
| 10 | [ -n "$omc" ] && [ -x "$omc" ] || { echo "test_check_candidate: AIEN_OMC is not an executable omc (NOT_RUN)" >&2; exit 3; } |
| 11 | tmp="$(mktemp -d)"; trap 'rm -rf "$tmp"' EXIT |
| 12 | chk="$tmp/check_candidate" |
| 13 | "$omc" < "$src" > "$chk" && chmod +x "$chk" || { echo "TEST FAIL: $src does not compile with $omc"; exit 1; } |
| 14 | stub="$tmp/stub.sh" |
| 15 | cat > "$stub" <<'S' |
| 16 | #!/usr/bin/env bash |
| 17 | [ "$2" != "deadbeefdeadbeefdeadbeefdeadbeefdeadbeef" ] |
| 18 | S |
| 19 | chmod +x "$stub"; export CAND_SHA_CHECK="$stub" |
| 20 | good="$here/testdata/cand-draft-fixture.toml" # the 2026-10-01 draft, kept as a fixture so these tests do not depend on the live manifest |
| 21 | live="$here/../qualification/candidates/CAND-0.toml" |
| 22 | pass=0; bad=0 |
| 23 | expect() { # expect <0|1> <label> <file> |
| 24 | "$chk" "$3" >/dev/null 2>&1; rc=$? |
| 25 | if [ "$rc" = "$1" ]; then pass=$((pass+1)); else bad=$((bad+1)); echo "TEST FAIL: $2 (rc=$rc)"; fi |
| 26 | } |
| 27 | expect 0 "CAND-0 valid" "$good" |
| 28 | sed '/^omega = /d' "$good" > "$tmp/a.toml"; expect 1 "missing omega" "$tmp/a.toml" |
| 29 | sed 's/^id = .*//' "$good" > "$tmp/b.toml"; expect 1 "missing id" "$tmp/b.toml" |
| 30 | sed 's/^omega = .*/omega = "abc123"/' "$good" > "$tmp/c.toml"; expect 1 "short sha" "$tmp/c.toml" |
| 31 | sed 's/^aienos = .*/aienos = "deadbeefdeadbeefdeadbeefdeadbeefdeadbeef"/' "$good" > "$tmp/d.toml"; expect 1 "nonexistent sha" "$tmp/d.toml" |
| 32 | sed 's/^status = .*/status = "frozen"/' "$good" > "$tmp/e.toml"; expect 1 "frozen with UNBUILT" "$tmp/e.toml" |
| 33 | sed '/^omega-runtime/d;/^aienos-boot/d;/^aien-proof =/d' "$good" > "$tmp/f.toml"; expect 1 "no executables" "$tmp/f.toml" |
| 34 | H64a=$(printf 'a%.0s' $(seq 64)); H64b=$(printf 'b%.0s' $(seq 64)); H64c=$(printf 'c%.0s' $(seq 64)) |
| 35 | Z64=$(printf '0%.0s' $(seq 64)) |
| 36 | frozen() { # frozen <out> : valid frozen manifest with real-looking digests |
| 37 | sed -e 's/^status = .*/status = "frozen"/' -e "s/^omega-runtime = .*/omega-runtime = \"$H64a\"/" \ |
| 38 | -e "s/^aienos-boot-image = .*/aienos-boot-image = \"$H64b\"/" -e "s/^aien-proof = .*/aien-proof = \"$H64c\"/" "$good" > "$1" |
| 39 | } |
| 40 | |
| 41 | # --- new: positive frozen case |
| 42 | frozen "$tmp/fz.toml"; expect 0 "frozen with real digests" "$tmp/fz.toml" |
| 43 | # --- new: all-zero digest rejected (frozen and draft) |
| 44 | sed "s/^aien-proof = .*/aien-proof = \"$Z64\"/" "$tmp/fz.toml" > "$tmp/z.toml"; expect 1 "all-zero digest (frozen)" "$tmp/z.toml" |
| 45 | sed "s/^aien-proof = .*/aien-proof = \"$Z64\"/" "$good" > "$tmp/z2.toml"; expect 1 "all-zero digest (draft)" "$tmp/z2.toml" |
| 46 | # --- new: '#' inside a quoted value is kept, not truncated |
| 47 | sed "s/^aien-proof = .*/aien-proof = \"${H64c}#frag\"/" "$tmp/fz.toml" > "$tmp/h.toml"; expect 1 "digest with # is not truncated into validity" "$tmp/h.toml" |
| 48 | sed 's/^note = .*/note = "issue #5 and #6" # trailing comment/' "$good" > "$tmp/n.toml"; expect 0 "# in note and trailing comment ok" "$tmp/n.toml" |
| 49 | # --- new: contracts pinned |
| 50 | sed '/^crumb-spec = /d' "$good" > "$tmp/k1.toml"; expect 1 "missing crumb-spec" "$tmp/k1.toml" |
| 51 | sed '/^spark-crumbs = /d' "$good" > "$tmp/k2.toml"; expect 1 "missing spark-crumbs" "$tmp/k2.toml" |
| 52 | sed 's/^spark-crumbs = .*/spark-crumbs = "deadbeefdeadbeefdeadbeefdeadbeefdeadbeef"/' "$good" > "$tmp/k3.toml"; expect 1 "nonexistent spark-crumbs sha" "$tmp/k3.toml" |
| 53 | sed 's/^crumb-spec = .*/crumb-spec = "abc"/' "$good" > "$tmp/k4.toml"; expect 1 "short crumb-spec sha" "$tmp/k4.toml" |
| 54 | |
| 55 | # --- new: real gh code path, via a faithful offline stub of `gh` on PATH. |
| 56 | # The stub accepts `gh api repos/aien-dev/<repo>/commits/<sha> -q .sha`, prints the sha and |
| 57 | # exits 0 for known shas, prints a 404 message and exits 1 for deadbeef..., and logs argv. |
| 58 | mkdir -p "$tmp/bin"; log="$tmp/gh.log" |
| 59 | cat > "$tmp/bin/gh" <<'S' |
| 60 | #!/usr/bin/env bash |
| 61 | echo "$*" >> "$GH_STUB_LOG" |
| 62 | [ "$1" = "api" ] || exit 2 |
| 63 | sha="${2##*/}" |
| 64 | if [ "$sha" = "deadbeefdeadbeefdeadbeefdeadbeefdeadbeef" ]; then echo "gh: Not Found (HTTP 404)" >&2; exit 1; fi |
| 65 | echo "$sha" |
| 66 | S |
| 67 | chmod +x "$tmp/bin/gh" |
| 68 | ghexpect() { # ghexpect <rc> <label> <file> |
| 69 | ( unset CAND_SHA_CHECK; PATH="$tmp/bin:$PATH" GH_STUB_LOG="$log" "$chk" "$3" >/dev/null 2>&1 ); rc=$? |
| 70 | if [ "$rc" = "$1" ]; then pass=$((pass+1)); else bad=$((bad+1)); echo "TEST FAIL: $2 (rc=$rc)"; fi |
| 71 | } |
| 72 | : > "$log" |
| 73 | ghexpect 0 "gh path: CAND-0 valid" "$good" |
| 74 | grep -q 'api repos/aien-dev/crumb-spec/commits/10b825133580f7a4388ce78aca64b5db52c5d35b ' "$log" \ |
| 75 | && pass=$((pass+1)) || { bad=$((bad+1)); echo "TEST FAIL: gh was not called for crumb-spec"; } |
| 76 | [ "$(wc -l < "$log")" -eq 7 ] && pass=$((pass+1)) || { bad=$((bad+1)); echo "TEST FAIL: expected 7 gh calls, got $(wc -l < "$log")"; } |
| 77 | ghexpect 1 "gh path: 404 sha rejected" "$tmp/d.toml" |
| 78 | ghexpect 1 "gh path: 404 contract sha rejected" "$tmp/k3.toml" |
| 79 | |
| 80 | |
| 81 | # --- live candidate manifest (CAND-0 is frozen): valid, 9 pins checked through the gh path, and its optional pins bite. |
| 82 | expect 0 "live CAND-0 valid" "$live" |
| 83 | grep -q '^status = "frozen"' "$live" && pass=$((pass+1)) || { bad=$((bad+1)); echo "TEST FAIL: live CAND-0 is not frozen"; } |
| 84 | : > "$log" |
| 85 | ghexpect 0 "gh path: live CAND-0 valid" "$live" |
| 86 | [ "$(wc -l < "$log")" -eq 9 ] && pass=$((pass+1)) || { bad=$((bad+1)); echo "TEST FAIL: expected 9 gh calls for the live manifest, got $(wc -l < "$log")"; } |
| 87 | sed 's/^physics = .*/physics = "abc123"/' "$live" > "$tmp/p1.toml"; expect 1 "short physics sha" "$tmp/p1.toml" |
| 88 | sed 's/^interplane = .*/interplane = "deadbeefdeadbeefdeadbeefdeadbeefdeadbeef"/' "$live" > "$tmp/p2.toml"; expect 1 "nonexistent interplane sha" "$tmp/p2.toml" |
| 89 | sed 's/^omega-runtime = .*/omega-runtime = "UNBUILT"/' "$live" > "$tmp/p3.toml"; expect 1 "frozen live manifest with UNBUILT" "$tmp/p3.toml" |
| 90 | |
| 91 | # --- amendments: <stem>.amendment-<n>.toml next to the manifest is read; a frozen manifest edit is refused. |
| 92 | amd="$tmp/amd"; mkdir -p "$amd"; frozen "$amd/CX.toml"; printf '\n[model]\nmodel-id = "m"\n' >> "$amd/CX.toml" |
| 93 | amend() { # amend <n> <manifest-sha> [extra line...] : write CX.amendment-<n>.toml |
| 94 | local n="$1" s="$2"; shift 2 |
| 95 | { printf 'schema = "CandidateAmendmentV1"\ncandidate = "CAND-0"\namendment = "%s"\ndate = "2026-10-06"\nreason = "test"\nmanifest-sha256 = "%s"\n\n[model]\n' "$n" "$s" |
| 96 | printf 'oracle-fixture-safetensors-sha256 = "%s"\n' "$H64a"; for l in "$@"; do printf '%s\n' "$l"; done; } > "$amd/CX.amendment-$n.toml" |
| 97 | } |
| 98 | MSHA=$(sha256sum "$amd/CX.toml" | cut -d' ' -f1) |
| 99 | amend 1 "$MSHA"; expect 0 "amendment: valid amendment accepted" "$amd/CX.toml" |
| 100 | "$chk" "$amd/CX.toml" 2>/dev/null | grep -q '(amendments: 1)' && pass=$((pass+1)) || { bad=$((bad+1)); echo "TEST FAIL: amendment file not picked up"; } |
| 101 | # Exact-byte amendment binding: parsing may drop NUL, hashing must not. |
| 102 | cp "$amd/CX.toml" "$amd/CX.raw-orig" |
| 103 | printf '\0' >> "$amd/CX.toml" |
| 104 | expect 1 "amendment: NUL-only frozen manifest edit refused" "$amd/CX.toml" |
| 105 | cp -r "$amd" "$tmp/amdnul" # original digest + altered bytes; retained for the mutant |
| 106 | NULSHA=$(sha256sum "$amd/CX.toml" | cut -d' ' -f1) |
| 107 | [ "$NULSHA" != "$MSHA" ] && pass=$((pass+1)) || { bad=$((bad+1)); echo "TEST FAIL: NUL fixture did not change raw digest"; } |
| 108 | amend 1 "$NULSHA" |
| 109 | expect 0 "amendment: actual raw NUL-containing manifest digest accepted" "$amd/CX.toml" |
| 110 | cp "$amd/CX.raw-orig" "$amd/CX.toml" |
| 111 | amend 1 "$MSHA" |
| 112 | amend 1 "$MSHA" "tokenizer-json-sha256 = \"$Z64\""; expect 1 "amendment: all-zero digest in amendment refused (file is read)" "$amd/CX.toml" |
| 113 | cp "$amd/CX.toml" "$amd/CX.orig"; amend 1 "$MSHA" |
| 114 | echo '# edited after the freeze' >> "$amd/CX.toml"; expect 1 "amendment: frozen manifest edited after amendment refused" "$amd/CX.toml" |
| 115 | cp "$amd/CX.orig" "$amd/CX.toml" |
| 116 | amend 1 "$MSHA" "[executables]" "omega-runtime = \"$H64b\""; expect 1 "amendment: [executables] cannot be amended" "$amd/CX.toml" |
| 117 | amend 1 "$MSHA" "[commits]" "physics = \"0123456789012345678901234567890123456789\""; expect 1 "amendment: [commits] cannot be amended" "$amd/CX.toml" |
| 118 | amend 1 "$MSHA" "model-id = \"other\""; expect 1 "amendment: a key already in the manifest is not overridden" "$amd/CX.toml" |
| 119 | cp -r "$amd" "$tmp/amdov" # kept for the override mutant below |
| 120 | amend 1 "$MSHA"; sed -i 's/^candidate = .*/candidate = "CAND-9"/' "$amd/CX.amendment-1.toml"; expect 1 "amendment: wrong candidate refused" "$amd/CX.toml" |
| 121 | amend 1 "$MSHA"; sed -i 's/^amendment = .*/amendment = "2"/' "$amd/CX.amendment-1.toml"; expect 1 "amendment: number must match file name" "$amd/CX.toml" |
| 122 | amend 1 "$MSHA" |
| 123 | live4="$here/../qualification/candidates/CAND-4.toml" |
| 124 | expect 0 "live CAND-4 with Amendment 1 valid" "$live4" |
| 125 | "$chk" "$live4" 2>/dev/null | grep -q '(amendments: 1)' && pass=$((pass+1)) || { bad=$((bad+1)); echo "TEST FAIL: live CAND-4 amendment not picked up"; } |
| 126 | [ -z "$(cd "$here/.." && git diff 7f99d7ee4221bb3cd8b3bc23bf167a64baac24e4 -- qualification/candidates/CAND-4.toml 2>/dev/null)" ] && pass=$((pass+1)) || { bad=$((bad+1)); echo "TEST FAIL: frozen CAND-4.toml differs from 7f99d7e"; } |
| 127 | echo '# edited' >> "$amd/CX.toml" # left edited for the mutant below |
| 128 | |
| 129 | # --- mutants. Each broken copy of the checker (one sed edit of the .om source, recompiled) must |
| 130 | # wrongly accept the bad input, proving the test above really depends on that check. |
| 131 | mutant() { # mutant <label> <sed-expr> <input-file> |
| 132 | sed "$2" "$src" > "$tmp/mut.om" |
| 133 | if cmp -s "$src" "$tmp/mut.om"; then bad=$((bad+1)); echo "TEST FAIL: mutant '$1' did not change the checker"; return; fi |
| 134 | "$omc" < "$tmp/mut.om" > "$tmp/mut" 2>/dev/null && chmod +x "$tmp/mut" || { bad=$((bad+1)); echo "TEST FAIL: mutant '$1' does not compile"; return; } |
| 135 | "$tmp/mut" "$3" >/dev/null 2>&1; rc=$? |
| 136 | if [ "$rc" = 0 ]; then pass=$((pass+1)); else bad=$((bad+1)); echo "TEST FAIL: mutant '$1' still rejected (rc=$rc), test does not bite"; fi |
| 137 | } |
| 138 | mutant "no zero-digest check" 's/^z = zero64 e$/z = false/' "$tmp/z.toml" |
| 139 | mutant "old # truncation" 's/^ret and lt i e ne get src i 34$/ret and lt i e and ne get src i 34 ne get src i 35/' "$tmp/h.toml" |
| 140 | mutant "contracts not required" 's/^const NREQ ulong 7$/const NREQ ulong 5/' "$tmp/k1.toml" |
| 141 | mutant "optional pins not checked" '/^optpins$/d' "$tmp/p1.toml" |
| 142 | mutant "optional pin sha existence not checked" '/^fn optpin /,/^fn optpins /s/^if not shaex i e$/if false/' "$tmp/p2.toml" |
| 143 | mutant "manifest pin not checked" 's/^if not mfeq e$/if false/' "$amd/CX.toml" |
| 144 | mutant "amendment may override" 's/^if inmf x$/if false/' "$tmp/amdov/CX.toml" |
| 145 | mutant "raw manifest hash normalized" 's/^sha256$/dropnul\nsha256/' "$tmp/amdnul/CX.toml" |
| 146 | echo "passed=$pass failed=$bad" |
| 147 | [ "$bad" -eq 0 ] |