interplane.Get Omega
Read in your language

omega-mv

Omega: a language for verified programs, semantic identity and machine realization. Seed-born work and existing implementations have distinct provenance.

Brought by drakestapleton

branch main Branches Tags History

omega-mv / tools / test_m19r_qualify.sh

204 lines · 12562 bytes Raw Blame History
1#!/bin/bash
2# SPDX-License-Identifier: Apache-2.0
3# Copyright 2026 Drake Stapleton (AIEN / aien-dev)
4#
5# Licensed under the Apache License, Version 2.0 (the "License");
6# you may not use this file except in compliance with the License.
7# You may obtain a copy of the License at
8#
9# http://www.apache.org/licenses/LICENSE-2.0
10#
11# Unless required by applicable law or agreed to in writing, software
12# distributed under the License is distributed on an "AS IS" BASIS,
13# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14# See the License for the specific language governing permissions and
15# limitations under the License.
16
17# test_m19r_qualify.sh -- negative tests for candidate binding, immutable
18# qualification emission and gate counting in tools/m19r_qualify.sh, plus
19# golden tests for the canonical JSON helper tools/json_canon.c.
20# Host only (no GPU). Run: make test-m19r-qualify (or run this file).
21set -u
22HERE=$(cd -P "$(dirname "$0")/.." && pwd)
23# shellcheck source=tools/m19r_qualify.sh
24. "$HERE/tools/m19r_qualify.sh"
25m19r_build_canon || { echo "FAIL: cannot build tools/json_canon.om"; exit 1; }
26TMP=$(mktemp -d)
27trap 'rm -rf "$TMP" "$M19R_TMP"' EXIT
28fails=0 passes=0
29ok() { echo " [PASS] $1"; passes=$((passes + 1)); }
30bad() { echo " [FAIL] $1"; fails=$((fails + 1)); }
31check() { if eval "$2"; then ok "$1"; else bad "$1"; fi; }
32
33echo "candidate mismatch and dirty tree"
34repo=$TMP/repo
35git init -q "$repo"
36echo candidate > "$repo/source.txt"
37git -C "$repo" add source.txt
38git -C "$repo" -c user.name=Test -c user.email=test@example.invalid commit -qm candidate
39sha=$(git -C "$repo" rev-parse HEAD)
40check "all-zero SHA rejected" '! m19r_must_candidate "$repo" 0000000000000000000000000000000000000000'
41check " with the HEAD message" '[ "$M19R_ERR" = "repo HEAD differs from supplied candidate" ]'
42check "short SHA rejected" '! m19r_must_candidate "$repo" "${sha:0:12}"'
43check " with the full-SHA message" '[ "$M19R_ERR" = "candidate must be an explicit full 40-hex SHA" ]'
44check "exact clean candidate accepted" 'm19r_must_candidate "$repo" "$sha"'
45check "upper-case candidate accepted" 'm19r_must_candidate "$repo" "${sha^^}"'
46echo dirty > "$repo/source.txt"
47check "dirty tree rejected" '! m19r_must_candidate "$repo" "$sha"'
48check " with the dirty message" '[ "$M19R_ERR" = "repo candidate tree is dirty" ]'
49git -C "$repo" checkout -q source.txt
50echo new > "$repo/untracked.txt"
51check "untracked file counts as dirty" '! m19r_must_candidate "$repo" "$sha"'
52
53echo "exclusive receipt and canonical digest"
54body='{"count": 2, "candidate": "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"}'
55h=$(printf '%s' "$body" | "$JSON_CANON" --sha256)
56check "digest of the canonical body" '[ "$h" = "$(printf "%s" "{\"candidate\":\"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\",\"count\":2}" | sha256sum | cut -d" " -f1)" ]'
57receipt="{\"receipt_digest\":\"$h\",${body#\{}"
58path=$TMP/M19R/$h.json
59check "first write succeeds" 'printf "%s" "$receipt" | m19r_write_immutable_receipt "$path"'
60original=$(sha256sum < "$path")
61check "file is read-only (0444)" '[ "$(stat -c %a "$path")" = 444 ]'
62check "second write refused" '! printf "%s" "$receipt" | m19r_write_immutable_receipt "$path"'
63check " as an existing file" '[ "$(cat "$M19R_TMP/error")" = "[Errno 17] File exists: '"'"'$path'"'"'" ]'
64check "original bytes unchanged" '[ "$(sha256sum < "$path")" = "$original" ]'
65
66echo "failed gate and derived final count"
67printf ' [PASS] GATE_ONE\n [FAIL] GATE_TWO\n' > "$TMP/gates.log"
68m19r_events "$TMP/gates.log" suite > "$TMP/seen.tsv"
69check "counts derived from observed lines" '[ "$(m19r_observed_counts < "$TMP/seen.tsv")" = "{\"completed\":2,\"failed\":1,\"passed\":1}" ]'
70check "a failed gate fails the run" '! m19r_require_passed < "$TMP/seen.tsv"'
71check "no observed gates fails the run" '! m19r_require_passed < /dev/null'
72head -n 1 "$TMP/seen.tsv" > "$TMP/one.tsv"
73check "first gate alone counts 1" '[ "$(m19r_observed_counts < "$TMP/one.tsv")" = "{\"completed\":1,\"failed\":0,\"passed\":1}" ]'
74check "first gate alone passes" 'm19r_require_passed < "$TMP/one.tsv"'
75printf 'SILICON_PASS: FAIL\nOTHER_PASS: measured true\n' > "$TMP/named.log"
76m19r_events "$TMP/named.log" named > "$TMP/named.tsv"
77check "named lines: FAIL word wins" '[ "$(cut -f3 "$TMP/named.tsv" | paste -sd, -)" = "FAIL,PASS" ]'
78check "named FAIL fails the run" '! m19r_require_passed < "$TMP/named.tsv"'
79printf 'XY_PASS: y\n [PASS]\n\n LATE_ID trailing\n[PASS]FOO\n[PASS] a:b-c(d)\n A__PASS: FAILED\nA_PASS: x\n' > "$TMP/order.log"
80check "gate lines first, id after blank lines, then named lines" '[ "$(m19r_events "$TMP/order.log" s | m19r_events_json)" = "[{\"suite\":\"s\",\"id\":\"LATE_ID\",\"status\":\"PASS\"},{\"suite\":\"s\",\"id\":\"a:b-c\",\"status\":\"PASS\"},{\"suite\":\"s\",\"id\":\"XY_PASS\",\"status\":\"PASS\"}]" ]'
81
82echo "tagged observations"
83printf 'M19_OBSERVED_JSON:{"a":1}\nnoise\nM19_OBSERVED_JSON:{"a":2}\n' > "$TMP/tag.log"
84check "every tagged line returned" '[ "$(m19r_tagged_json "$TMP/tag.log" M19_OBSERVED_JSON | tail -n 1)" = "{\"a\":2}" ]'
85check "missing tag fails" '! m19r_tagged_json "$TMP/tag.log" M19R_SOAK_JSON > /dev/null'
86printf 'M19R_SOAK_JSON:{"a":\n' > "$TMP/badtag.log"
87check "malformed tagged JSON fails" '! m19r_tagged_json "$TMP/badtag.log" M19R_SOAK_JSON > /dev/null'
88
89echo "canonical JSON matches Python json.dumps"
90trap_doc='{"z":[84.830,85.000,1e3,0,-0,-0.0,18446744073709551615,1e16,1e-05,0.0001,1.5e-7],"b":{"e":{},"f":[]},"s":"q\"b\\s\u0001\n\u00e9\u007f\ud83d\ude00","a":1,"a":"dup-last"}'
91compact=$(printf '%s' "$trap_doc" | "$JSON_CANON")
92want=$(printf '{"a":"dup-last","b":{"e":{},"f":[]},"s":"q\\"b\\\\s\\u0001\\n\xc3\xa9\x7f\xf0\x9f\x98\x80","z":[84.83,85.0,1000.0,0,0,-0.0,18446744073709551615,1e+16,1e-05,0.0001,1.5e-07]}')
93check "compact form (numbers, escapes, raw UTF-8, dup key)" '[ "$compact" = "$want" ]'
94pretty=$(printf '%s' "$trap_doc" | "$JSON_CANON" --pretty)
95want_pretty='{
96 "a": "dup-last",
97 "b": {
98 "e": {},
99 "f": []
100 },
101 "s": "q\"b\\s\u0001\n\u00e9\u007f\ud83d\ude00",
102 "z": [
103 84.83,
104 85.0,
105 1000.0,
106 0,
107 0,
108 -0.0,
109 18446744073709551615,
110 1e+16,
111 1e-05,
112 0.0001,
113 1.5e-07
114 ]
115}'
116check "pretty form (indent 2, ASCII escapes)" '[ "$pretty" = "$want_pretty" ]'
117check "invalid JSON rejected" '! printf "{\"a\":1,}" | "$JSON_CANON" > /dev/null 2>&1'
118rec=$(ls "$HERE"/evidence/M19R/*.json | head -n 1)
119name=$(basename "$rec" .json)
120check "recorded M19R receipt digest reproduced" '[ "$(jq "del(.receipt_digest)" "$rec" | "$JSON_CANON" --sha256)" = "$name" ]'
121check "recorded M19R receipt pretty form reproduced" '"$JSON_CANON" --pretty < "$rec" | cmp -s - "$rec"'
122
123echo "evidence retention across runs (stubbed qualification, no hardware)"
124check "make clean removes only the build directory" '[ "$(make --no-print-directory -C "$HERE" -n clean | tr -d "\n")" = "rm -rf ${OUT_DIR:-build}" ]'
125fake=$TMP/fakeomega
126mkdir -p "$fake/tools" "$fake/build"
127ln -s "$HERE/seed" "$fake/seed"; cp "$HERE/tools/json_canon.om" "$fake/tools/"
128printf "/qual-runs/\nseed\n" > "$fake/.gitignore"
129printf 'clean:\n\trm -rf build\n' > "$fake/Makefile"
130git init -q "$fake"
131fake_run() { # fake_run EXTRA_ARGS... ; runs m19r_main with a stubbed qualify
132 (
133 M19R_OMEGA=$fake
134 m19r_qualify() { # like the real flow: make clean, then write evidence into the run dir
135 m19r_cmd "$M19R_OMEGA" - make clean || return 1
136 mkdir -p "$M19R_OMEGA/build"; echo product > "$M19R_OMEGA/build/omegatool"
137 printf ' [PASS] GATE_A\n' > "$M19R_RUN_DIR/m19.log"
138 echo "stub stdout line"; echo "stub stderr line" >&2
139 R_STATUS=$STUB_STATUS R_EVENTS='[]' R_M19='{}'
140 [ "$STUB_STATUS" = PASS ] || { m19r_fail "stub failure"; return 1; }
141 }
142 m19r_main --omega-candidate "$(printf 'a%.0s' {1..40})" --physics-candidate "$(printf 'b%.0s' {1..40})" \
143 --physics-dir "$TMP/nophysics" "$@"
144 ) > "$TMP/fake.out" 2> "$TMP/fake.err"
145}
146STUB_STATUS=PASS fake_run --run-id run1; rc1=$?
147r1=$fake/qual-runs/run1
148check "run 1 exits 0 and writes under <omega>/qual-runs (default root)" '[ "$rc1" = 0 ] && [ -f "$r1/run.json" ]'
149check "run 1 has command, environment, logs, verdict, hashes" 'for f in command.txt environment.json stdout.log stderr.log verdict.json hashes.sha256 m19.log; do [ -f "$r1/$f" ] || exit 1; done'
150check "stdout.log/stderr.log captured the run" 'grep -q "stub stdout line" "$r1/stdout.log" && grep -q "stub stderr line" "$r1/stderr.log"'
151check "environment.json has the six fields" '[ "$(jq -r "[.hostname,.date_utc,.omega_sha,.physics_sha,.uname_r,.user] | map(length > 0) | all" "$r1/environment.json")" = true ]'
152check "verdict.json says PASS, exit 0" '[ "$(jq -r "[.status,.exit_code,.run_id] | @tsv" "$r1/verdict.json")" = "$(printf "PASS\t0\trun1")" ]'
153check "hashes.sha256 verifies and omits itself" '(cd "$r1" && sha256sum -c --quiet hashes.sha256) && ! grep -q hashes.sha256 "$r1/hashes.sha256"'
154count1=$(find "$r1" -type f | wc -l)
155sums1=$(cd "$r1" && find . -type f -print0 | sort -z | xargs -0 sha256sum)
156STUB_STATUS=PASS fake_run --run-id run2; rc2=$?
157check "run 2 (which runs make clean) exits 0" '[ "$rc2" = 0 ] && [ -f "$fake/qual-runs/run2/run.json" ]'
158check "run 2 cleanup removed build products but not run 1" '[ -d "$r1" ] && [ -f "$fake/build/omegatool" ]'
159check "run 1 hashes still verify after run 2" '(cd "$r1" && sha256sum -c --quiet hashes.sha256)'
160check "run 1 file count unchanged" '[ "$(find "$r1" -type f | wc -l)" = "$count1" ]'
161check "run 1 bytes identical (every file)" '[ "$(cd "$r1" && find . -type f -print0 | sort -z | xargs -0 sha256sum)" = "$sums1" ]'
162STUB_STATUS=PASS fake_run --run-id run1; rcx=$?
163check "reusing an existing run id exits 2" '[ "$rcx" = 2 ] && grep -q "never reused" "$TMP/fake.err"'
164check " and leaves run 1 untouched" '(cd "$r1" && sha256sum -c --quiet hashes.sha256) && [ "$(find "$r1" -type f | wc -l)" = "$count1" ]'
165STUB_STATUS=PASS fake_run --run-id inbuild --evidence-root "$fake/build/qual"; rcb=$?
166check "evidence root inside build/ refused (exit 2)" '[ "$rcb" = 2 ]'
167STUB_STATUS=FAILED fake_run --run-id run3; rc3=$?
168check "failed run exits 1 with FAILED verdict and exit code" '[ "$rc3" = 1 ] && [ "$(jq -r "[.status,.exit_code,.failing_step,.failing_gate_or_exit] | @tsv" "$fake/qual-runs/run3/verdict.json")" = "$(printf "FAILED\t1\tstub failure\texit:1")" ]'
169check "command.txt records the exact argv" 'grep -q -- "--run-id run3" "$fake/qual-runs/run3/command.txt"'
170
171echo "campaign verdicts (tools/m19r_campaign.sh)"
172CAMP=$HERE/tools/m19r_campaign.sh
173mkrun() { # mkrun DIR ID STATUS -- a sealed run directory
174 local d=$1/$2
175 mkdir -p "$d"
176 printf '{"status":"%s"}' "$3" > "$d/run.json"
177 printf '{"run_id":"%s","status":"%s","exit_code":0}' "$2" "$3" > "$d/verdict.json"
178 echo "log $2" > "$d/m19.log"
179 m19r_seal "$d"
180}
181verdict() { "$CAMP" --campaign-dir "$1" --runs "$2" > /dev/null 2>&1; jq -r .verdict "$1/final-verdict.json"; }
182c=$TMP/c_pass; mkdir "$c"; mkrun "$c" r1 PASS; mkrun "$c" r2 PASS
183check "PASS: 2 of 2 runs PASS" '[ "$(verdict "$c" 2)" = PASS ]'
184check " campaign.json written with sha, rule, created_at" '[ "$(jq -r "[.sha,.rule,.created_at] | map(length > 0) | all" "$c/campaign.json")" = true ]'
185check " exits 0" '"$CAMP" --campaign-dir "$c" --runs 2 > /dev/null'
186check "INCOMPLETE: 2 usable runs but 3 required" '[ "$(verdict "$c" 3)" = INCOMPLETE ]'
187check " exits 1" '! "$CAMP" --campaign-dir "$c" --runs 3 > /dev/null'
188c=$TMP/c_fail; mkdir "$c"; mkrun "$c" r1 PASS; mkrun "$c" r2 FAILED
189check "FAIL: one run FAILED" '[ "$(verdict "$c" 2)" = FAIL ]'
190c=$TMP/c_inv; mkdir "$c"; mkrun "$c" r1 PASS; mkrun "$c" r2 PASS
191chmod u+w "$c/r2/m19.log"; printf 'log r3' > "$c/r2/m19.log"
192check "INVALID: one altered byte in a run file" '[ "$(verdict "$c" 2)" = INVALID ]'
193c=$TMP/c_inv2; mkdir "$c"; mkrun "$c" r1 PASS; mkrun "$c" r2 PASS; rm "$c/r2/verdict.json"
194check "INVALID: run lacks verdict.json" '[ "$(verdict "$c" 2)" = INVALID ]'
195c=$TMP/c_inv3; mkdir "$c"; mkrun "$c" r1 PASS; mkrun "$c" r2 FAILED; chmod u+w "$c/r1/m19.log"; echo x >> "$c/r1/m19.log"
196check "INVALID takes precedence over FAIL" '[ "$(verdict "$c" 2)" = INVALID ]'
197check "bad arguments exit 2" '"$CAMP" --campaign-dir "$TMP/nonexistent" --runs 2 > /dev/null 2>&1; [ $? = 2 ]'
198
199printf 'TOTAL GATES: %d | PASSED: %d | FAILED: %d\n' "$((passes + fails))" "$passes" "$fails"
200if [ "$fails" -ne 0 ]; then
201 echo "M19R qualifier tests: $fails FAILED"
202 exit 1
203fi
204echo "M19R qualifier tests: all passed"