interplane.Get Omega
Read in your language

aien-architecture-mv

Canonical architecture for the AIEN ecosystem: flows, boundaries, ADRs, and the map from design to implementation repositories.

Brought by drakestapleton

branch main Branches Tags History

aien-architecture-mv / scripts / test_check_candidate.sh

147 lines · 11013 bytes Raw Blame History
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.
6set -u
7here="$(cd "$(dirname "$0")" && pwd)"
8src="$here/../tools/candidate/check_candidate.om"
9omc="${AIEN_OMC:-}"
10[ -n "$omc" ] && [ -x "$omc" ] || { echo "test_check_candidate: AIEN_OMC is not an executable omc (NOT_RUN)" >&2; exit 3; }
11tmp="$(mktemp -d)"; trap 'rm -rf "$tmp"' EXIT
12chk="$tmp/check_candidate"
13"$omc" < "$src" > "$chk" && chmod +x "$chk" || { echo "TEST FAIL: $src does not compile with $omc"; exit 1; }
14stub="$tmp/stub.sh"
15cat > "$stub" <<'S'
16#!/usr/bin/env bash
17[ "$2" != "deadbeefdeadbeefdeadbeefdeadbeefdeadbeef" ]
18S
19chmod +x "$stub"; export CAND_SHA_CHECK="$stub"
20good="$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
21live="$here/../qualification/candidates/CAND-0.toml"
22pass=0; bad=0
23expect() { # 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}
27expect 0 "CAND-0 valid" "$good"
28sed '/^omega = /d' "$good" > "$tmp/a.toml"; expect 1 "missing omega" "$tmp/a.toml"
29sed 's/^id = .*//' "$good" > "$tmp/b.toml"; expect 1 "missing id" "$tmp/b.toml"
30sed 's/^omega = .*/omega = "abc123"/' "$good" > "$tmp/c.toml"; expect 1 "short sha" "$tmp/c.toml"
31sed 's/^aienos = .*/aienos = "deadbeefdeadbeefdeadbeefdeadbeefdeadbeef"/' "$good" > "$tmp/d.toml"; expect 1 "nonexistent sha" "$tmp/d.toml"
32sed 's/^status = .*/status = "frozen"/' "$good" > "$tmp/e.toml"; expect 1 "frozen with UNBUILT" "$tmp/e.toml"
33sed '/^omega-runtime/d;/^aienos-boot/d;/^aien-proof =/d' "$good" > "$tmp/f.toml"; expect 1 "no executables" "$tmp/f.toml"
34H64a=$(printf 'a%.0s' $(seq 64)); H64b=$(printf 'b%.0s' $(seq 64)); H64c=$(printf 'c%.0s' $(seq 64))
35Z64=$(printf '0%.0s' $(seq 64))
36frozen() { # 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
42frozen "$tmp/fz.toml"; expect 0 "frozen with real digests" "$tmp/fz.toml"
43# --- new: all-zero digest rejected (frozen and draft)
44sed "s/^aien-proof = .*/aien-proof = \"$Z64\"/" "$tmp/fz.toml" > "$tmp/z.toml"; expect 1 "all-zero digest (frozen)" "$tmp/z.toml"
45sed "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
47sed "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"
48sed '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
50sed '/^crumb-spec = /d' "$good" > "$tmp/k1.toml"; expect 1 "missing crumb-spec" "$tmp/k1.toml"
51sed '/^spark-crumbs = /d' "$good" > "$tmp/k2.toml"; expect 1 "missing spark-crumbs" "$tmp/k2.toml"
52sed 's/^spark-crumbs = .*/spark-crumbs = "deadbeefdeadbeefdeadbeefdeadbeefdeadbeef"/' "$good" > "$tmp/k3.toml"; expect 1 "nonexistent spark-crumbs sha" "$tmp/k3.toml"
53sed '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.
58mkdir -p "$tmp/bin"; log="$tmp/gh.log"
59cat > "$tmp/bin/gh" <<'S'
60#!/usr/bin/env bash
61echo "$*" >> "$GH_STUB_LOG"
62[ "$1" = "api" ] || exit 2
63sha="${2##*/}"
64if [ "$sha" = "deadbeefdeadbeefdeadbeefdeadbeefdeadbeef" ]; then echo "gh: Not Found (HTTP 404)" >&2; exit 1; fi
65echo "$sha"
66S
67chmod +x "$tmp/bin/gh"
68ghexpect() { # 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"
73ghexpect 0 "gh path: CAND-0 valid" "$good"
74grep -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")"; }
77ghexpect 1 "gh path: 404 sha rejected" "$tmp/d.toml"
78ghexpect 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.
82expect 0 "live CAND-0 valid" "$live"
83grep -q '^status = "frozen"' "$live" && pass=$((pass+1)) || { bad=$((bad+1)); echo "TEST FAIL: live CAND-0 is not frozen"; }
84: > "$log"
85ghexpect 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")"; }
87sed 's/^physics = .*/physics = "abc123"/' "$live" > "$tmp/p1.toml"; expect 1 "short physics sha" "$tmp/p1.toml"
88sed 's/^interplane = .*/interplane = "deadbeefdeadbeefdeadbeefdeadbeefdeadbeef"/' "$live" > "$tmp/p2.toml"; expect 1 "nonexistent interplane sha" "$tmp/p2.toml"
89sed '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.
92amd="$tmp/amd"; mkdir -p "$amd"; frozen "$amd/CX.toml"; printf '\n[model]\nmodel-id = "m"\n' >> "$amd/CX.toml"
93amend() { # 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}
98MSHA=$(sha256sum "$amd/CX.toml" | cut -d' ' -f1)
99amend 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.
102cp "$amd/CX.toml" "$amd/CX.raw-orig"
103printf '\0' >> "$amd/CX.toml"
104expect 1 "amendment: NUL-only frozen manifest edit refused" "$amd/CX.toml"
105cp -r "$amd" "$tmp/amdnul" # original digest + altered bytes; retained for the mutant
106NULSHA=$(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"; }
108amend 1 "$NULSHA"
109expect 0 "amendment: actual raw NUL-containing manifest digest accepted" "$amd/CX.toml"
110cp "$amd/CX.raw-orig" "$amd/CX.toml"
111amend 1 "$MSHA"
112amend 1 "$MSHA" "tokenizer-json-sha256 = \"$Z64\""; expect 1 "amendment: all-zero digest in amendment refused (file is read)" "$amd/CX.toml"
113cp "$amd/CX.toml" "$amd/CX.orig"; amend 1 "$MSHA"
114echo '# edited after the freeze' >> "$amd/CX.toml"; expect 1 "amendment: frozen manifest edited after amendment refused" "$amd/CX.toml"
115cp "$amd/CX.orig" "$amd/CX.toml"
116amend 1 "$MSHA" "[executables]" "omega-runtime = \"$H64b\""; expect 1 "amendment: [executables] cannot be amended" "$amd/CX.toml"
117amend 1 "$MSHA" "[commits]" "physics = \"0123456789012345678901234567890123456789\""; expect 1 "amendment: [commits] cannot be amended" "$amd/CX.toml"
118amend 1 "$MSHA" "model-id = \"other\""; expect 1 "amendment: a key already in the manifest is not overridden" "$amd/CX.toml"
119cp -r "$amd" "$tmp/amdov" # kept for the override mutant below
120amend 1 "$MSHA"; sed -i 's/^candidate = .*/candidate = "CAND-9"/' "$amd/CX.amendment-1.toml"; expect 1 "amendment: wrong candidate refused" "$amd/CX.toml"
121amend 1 "$MSHA"; sed -i 's/^amendment = .*/amendment = "2"/' "$amd/CX.amendment-1.toml"; expect 1 "amendment: number must match file name" "$amd/CX.toml"
122amend 1 "$MSHA"
123live4="$here/../qualification/candidates/CAND-4.toml"
124expect 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"; }
127echo '# 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.
131mutant() { # 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}
138mutant "no zero-digest check" 's/^z = zero64 e$/z = false/' "$tmp/z.toml"
139mutant "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"
140mutant "contracts not required" 's/^const NREQ ulong 7$/const NREQ ulong 5/' "$tmp/k1.toml"
141mutant "optional pins not checked" '/^optpins$/d' "$tmp/p1.toml"
142mutant "optional pin sha existence not checked" '/^fn optpin /,/^fn optpins /s/^if not shaex i e$/if false/' "$tmp/p2.toml"
143mutant "manifest pin not checked" 's/^if not mfeq e$/if false/' "$amd/CX.toml"
144mutant "amendment may override" 's/^if inmf x$/if false/' "$tmp/amdov/CX.toml"
145mutant "raw manifest hash normalized" 's/^sha256$/dropnul\nsha256/' "$tmp/amdnul/CX.toml"
146echo "passed=$pass failed=$bad"
147[ "$bad" -eq 0 ]