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_numeric_qualify.sh

426 lines · 32263 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_numeric_qualify.sh -- host-only tests of the Gate 5 (OMEGA-NUMERIC-0)
18# qualifier in tests/run_numeric_gates.sh: physics pin checks, gate log
19# checks and the immutable digest-named receipt writer. No GPU, no device.
20# Everything is written under a temporary directory, never into evidence/.
21# The "hardware" descriptors here are made up for the test and say so
22# (UNIT_TEST_FAKE_NOT_HARDWARE); they are never from a device.
23# Run: make test-numeric-qualify (or run this file).
24set -u
25HERE=$(cd -P "$(dirname "$0")/.." && pwd)
26# shellcheck source=tests/run_numeric_gates.sh
27. "$HERE/tests/run_numeric_gates.sh"
28m19r_build_canon || { echo "FAIL: cannot build tools/json_canon.c"; exit 1; }
29TMP=$(mktemp -d)
30trap 'chmod -R u+w "$TMP"; rm -rf "$TMP" "$M19R_TMP"' EXIT
31fails=0
32ok() { echo " [PASS] $1"; }
33bad() { echo " [FAIL] $1"; fails=$((fails + 1)); }
34check() { if eval "$2"; then ok "$1"; else bad "$1"; fi; [ -z "${SHOW_ERR:-}" ] || echo " ($M19R_ERR)"; }
35gitc() { git -C "$1" -c user.name=Test -c user.email=test@example.invalid "${@:2}"; }
36
37echo "physics dependency comes from physics.lock"
38phys=$TMP/physics
39git init -q "$phys"
40echo old > "$phys/README"
41gitc "$phys" add README && gitc "$phys" commit -qm "no forge yet"
42old=$(git -C "$phys" rev-parse HEAD)
43omega=$TMP/omega
44git init -q "$omega"
45echo "$old" > "$omega/physics.lock"
46gitc "$omega" add physics.lock && gitc "$omega" commit -qm lock
47check "pinned commit without forge/ refused" '! num_check_physics "$omega" "$phys" "$old"'
48check " message names forge and the owner decision" '[[ $M19R_ERR == *"has no forge/forge_descriptor.h"*"owner decision"* ]]'
49mkdir -p "$phys/forge" "$phys/nvrm" "$phys/m16"
50for f in forge/forge_descriptor.h forge/forge_descriptor.c forge/forge_realize.c sha256_clean.c nvrm/nvrm.c m16/m16_native.c; do
51 echo "/* stub */" > "$phys/$f"
52done
53gitc "$phys" add -A && gitc "$phys" commit -qm "forge"
54new=$(git -C "$phys" rev-parse HEAD)
55check "candidate differing from physics.lock refused" '! num_check_physics "$omega" "$phys" "$new"'
56check " message shows both commits" '[ "$M19R_ERR" = "physics.lock pins $old but --physics-candidate is $new" ]'
57check "checkout not at the pinned commit refused" '! num_check_physics "$omega" "$phys" "$old"'
58check " message shows both commits" '[ "$M19R_ERR" = "physics checkout $phys is at $new but physics.lock pins $old" ]'
59check "missing physics checkout refused" '! num_check_physics "$omega" "$TMP/nowhere" "$old"'
60echo "$new" > "$omega/physics.lock"
61gitc "$omega" commit -qam "move pin"
62check "matching pin with forge/ accepted" 'num_check_physics "$omega" "$phys" "$new"'
63echo dirty >> "$phys/README"
64check "dirty physics tree refused" '! num_check_physics "$omega" "$phys" "$new"'
65gitc "$phys" checkout -q README
66echo "not-a-sha" > "$omega/physics.lock"
67check "malformed physics.lock refused" '! num_check_physics "$omega" "$phys" "$new"'
68echo "$new" > "$omega/physics.lock"
69
70echo "gate log checks (comparison mode from the manifest, full launches, typed counts)"
71fake_digest=$(printf 'UNIT_TEST_FAKE_NOT_HARDWARE' | sha256sum | cut -d' ' -f1)
72LOG_BIN_SHA=$(printf 'unit-test-binary' | sha256sum | cut -d' ' -f1)
73LOG_RUN_ID=unit-test-run
74mode_of() { printf '%s\n' $NUM_OP_MANIFEST | awk -F: -v o="$1" '$1 == o { print $2 }'; }
75# good_log -- what a passing chip run prints. The run line is the binary's
76# own (run id + digest of /proc/self/exe); everything else is made up here.
77good_log() {
78 local id op mode c
79 echo "OMEGA_NUMERIC_RUN_JSON:{\"run_id\":\"$LOG_RUN_ID\",\"binary_sha256\":\"$LOG_BIN_SHA\"}"
80 for op in $NUM_ENCODED_OPS; do
81 mode=$(mode_of "$op")
82 echo "OMEGA_NUMERIC_REGISTRY_JSON:{\"op\":\"$op\",\"encoded\":true,\"compare\":\"$mode\",\"launches\":$([ "$op" = FFMA ] && echo 8 || echo 1)}"
83 done
84 for id in $NUM_EXPECTED_IDS; do echo "[PASS] $id"; done
85 echo "OMEGA_NUMERIC_HWDESC_JSON:{\"source\":\"FORGE_PROBE\",\"descriptor_digest\":\"$fake_digest\",\"derived_alias\":\"UNIT_TEST_FAKE_NOT_HARDWARE\"}"
86 for op in $NUM_ENCODED_OPS; do
87 mode=$(mode_of "$op")
88 case $mode in
89 SEED_BOUND) echo "OMEGA_NUMERIC_PARITY_JSON:{\"op\":\"$op\",\"tier\":\"gb10\",\"compare\":\"SEED_BOUND\",\"n\":4096,\"checked\":3000,\"out_of_bound\":0,\"skipped\":1096}";;
90 *) if [ "$op" = FFMA ]; then
91 for c in $NUM_FFMA_C; do
92 echo "OMEGA_NUMERIC_PARITY_JSON:{\"op\":\"FFMA\",\"tier\":\"gb10\",\"compare\":\"$mode\",\"c_bits\":\"$c\",\"n\":4096,\"checked\":4096,\"mismatches\":0}"
93 done
94 elif [ "$op" = REDUCE_SUM ]; then
95 echo "OMEGA_NUMERIC_PARITY_JSON:{\"op\":\"REDUCE_SUM\",\"tier\":\"gb10\",\"compare\":\"$mode\",\"reduction_order\":\"$NUM_REDUCE_ORDER\",\"n\":4096,\"checked\":128,\"mismatches\":0}"
96 else
97 echo "OMEGA_NUMERIC_PARITY_JSON:{\"op\":\"$op\",\"tier\":\"gb10\",\"compare\":\"$mode\",\"n\":4096,\"checked\":4096,\"mismatches\":0}"
98 fi;;
99 esac
100 echo "OMEGA_NUMERIC_PARITY_JSON:{\"op\":\"$op\",\"tier\":\"cpu\",\"compare\":\"BIT_EXACT\",\"n\":4096,\"checked\":4096,\"mismatches\":0}"
101 done
102}
103good_log > "$TMP/good.log"
104# gb10 LOG SED -- the good log with one sed edit applied to the GB10 lines only.
105gb10() { sed "/\"tier\":\"gb10\"/{
106$2
107}" "$TMP/good.log" > "$TMP/$1.log"
108 cmp -s "$TMP/good.log" "$TMP/$1.log" && bad "fixture $1: the edit changed nothing"; }
109check "complete passing log accepted" 'num_check_log "$TMP/good.log"'
110check " hardware digest read from the log" '[ "$NUM_HWDIGEST" = "$fake_digest" ]'
111check " 49 GB10 parity lines kept (FFMA x8 + 41 ops)" '[ "$(printf "%s" "$NUM_PARITY" | jq length)" = 49 ]'
112check " run id and binary digest read from the run line" '[ "$NUM_LOG_RUN_ID" = "$LOG_RUN_ID" ] && [ "$NUM_LOG_BINARY_SHA" = "$LOG_BIN_SHA" ]'
113sed 's/"source":"FORGE_PROBE"/"source":"FAKE_NON_HARDWARE_CPU_ONLY","fake":true/' "$TMP/good.log" > "$TMP/fake.log"
114check "CPU-only fake descriptor refused" '! num_check_log "$TMP/fake.log"'
115check " as not a device probe" '[[ $M19R_ERR == "hardware descriptor is not a device probe"* ]]'
116sed 's/"derived_alias"/"fake":true,"derived_alias"/' "$TMP/good.log" > "$TMP/fake2.log"
117check "descriptor marked fake refused" '! num_check_log "$TMP/fake2.log"'
118sed "s/$fake_digest/$(printf '0%.0s' {1..64})/" "$TMP/good.log" > "$TMP/zero.log"
119check "all-zero descriptor digest refused" '! num_check_log "$TMP/zero.log"'
120grep -v '"op":"SHFL_DOWN","tier":"gb10"' "$TMP/good.log" > "$TMP/noshfl.log"
121check "missing GB10 parity line refused" '! num_check_log "$TMP/noshfl.log"'
122check " naming the op" '[ "$M19R_ERR" = "no GB10 parity line for SHFL_DOWN" ]'
123gb10 onebit '/"op":"FFMA".*"c_bits":"0x7f800000"/s/"mismatches":0/"mismatches":1/'
124check "one GB10 mismatch in one FFMA launch refused" '! num_check_log "$TMP/onebit.log"'
125gb10 err '/"op":"FADD"/s/"n":4096.*}/"error":-4}/'
126check "GB10 launch error refused" '! num_check_log "$TMP/err.log"'
127gb10 mufu '/"op":"MUFU_RCP"/s/"out_of_bound":0/"out_of_bound":3/'
128check "MUFU seed out of bound refused" '! num_check_log "$TMP/mufu.log"'
129sed 's/^\[PASS\] FP32_SIMT_OPCODES_ENCODED/[FAIL] FP32_SIMT_OPCODES_ENCODED/' "$TMP/good.log" > "$TMP/fail.log"
130check "a failed test refused" '! num_check_log "$TMP/fail.log"'
131grep -v 'NEG_FTZ_DETECTED_AND_REJECTED' "$TMP/good.log" > "$TMP/miss.log"
132check "missing expected test refused" '! num_check_log "$TMP/miss.log"'
133{ cat "$TMP/good.log"; echo "[PASS] CPU_GB10_BIT_PARITY"; } > "$TMP/dup.log"
134check "duplicated test refused" '! num_check_log "$TMP/dup.log"'
135{ cat "$TMP/good.log"; echo "[SKIP] CPU_GB10_BIT_PARITY (needs GB10)"; } > "$TMP/skip.log"
136check "any SKIP refused" '! num_check_log "$TMP/skip.log"'
137{ cat "$TMP/good.log"; echo "[PASS] EXTRA_UNLISTED_TEST"; } > "$TMP/extra.log"
138check "unlisted extra test refused" '! num_check_log "$TMP/extra.log"'
139# Finding 5: the log does not choose its own comparison.
140gb10 relabel '/"op":"FADD"/s/"compare":"BIT_EXACT","n":4096,"checked":4096,"mismatches":0/"compare":"SEED_BOUND","n":4096,"checked":4096,"mismatches":4096,"out_of_bound":0,"skipped":0/'
141check "FADD relabelled SEED_BOUND with 4096 mismatches refused" '! num_check_log "$TMP/relabel.log"'
142check " comparison taken from the manifest" '[ "$M19R_ERR" = "GB10 parity for FADD: comparison \"SEED_BOUND\" but the manifest says BIT_EXACT" ]'
143gb10 relabel2 '/"op":"FSEL"/s/"compare":"INT_EXACT"/"compare":"BIT_EXACT"/'
144check "FSEL relabelled BIT_EXACT refused" '! num_check_log "$TMP/relabel2.log"'
145gb10 relabel3 '/"op":"F32_TO_F16"/s/"compare":"F16_BITS"/"compare":"BIT_EXACT"/'
146check "F32_TO_F16 relabelled BIT_EXACT refused (manifest says F16_BITS)" '! num_check_log "$TMP/relabel3.log"'
147gb10 e1mis '/"op":"F32_TO_BF16"/s/"mismatches":0/"mismatches":3/'
148check "F32_TO_BF16 with 3 mismatches refused" '! num_check_log "$TMP/e1mis.log"'
149gb10 e1gone '/"op":"FFMA_V"/d'
150check "missing FFMA_V parity line refused" '! num_check_log "$TMP/e1gone.log"'
151gb10 strn '/"op":"FMUL"/s/"n":4096,"checked":4096/"n":"4096","checked":"4096"/'
152check "string-typed counts refused" '! num_check_log "$TMP/strn.log"'
153gb10 strm '/"op":"FMUL"/s/"mismatches":0/"mismatches":"0"/'
154check "string-typed mismatches refused" '! num_check_log "$TMP/strm.log"'
155gb10 frac '/"op":"FMUL"/s/"mismatches":0/"mismatches":0.5/'
156check "fractional count refused" '! num_check_log "$TMP/frac.log"'
157# Finding 6: full corpus, every launch, exact identities and multiplicities.
158gb10 small '/"op":"FADD"/s/"n":4096,"checked":4096/"n":1,"checked":1/'
159check "n=1 checked=1 refused" '! num_check_log "$TMP/small.log"'
160gb10 partial '/"op":"FADD"/s/"checked":4096/"checked":4095/'
161check "partial check (4095 of 4096) refused" '! num_check_log "$TMP/partial.log"'
162gb10 seedsum '/"op":"MUFU_RSQ"/s/"skipped":1096/"skipped":1000/'
163check "seed-bound checked+skipped != n refused" '! num_check_log "$TMP/seedsum.log"'
164gb10 seedzero '/"op":"MUFU_RSQ"/s/"checked":3000,"out_of_bound":0,"skipped":1096/"checked":0,"out_of_bound":0,"skipped":4096/'
165check "seed-bound with nothing checked refused" '! num_check_log "$TMP/seedzero.log"'
166grep -v '"op":"FFMA","tier":"gb10".*"c_bits":"0x\(80000000\|00800000\|bf800000\|7fc00000\|7f800000\|00000003\|80800000\)"' "$TMP/good.log" > "$TMP/ffma1.log"
167check "a single FFMA launch refused" '! num_check_log "$TMP/ffma1.log"'
168check " naming the missing launches" '[[ $M19R_ERR == "FFMA launches [\"0x3f800000\"], need exactly one per c in"* ]]'
169gb10 ffmadup '/"op":"FFMA".*"c_bits":"0x80800000"/s/0x80800000/0x3f800000/'
170check "a duplicated FFMA c (7 distinct of 8) refused" '! num_check_log "$TMP/ffmadup.log"'
171{ cat "$TMP/good.log"; grep '"op":"FFMA","tier":"gb10".*"c_bits":"0x7fc00000"' "$TMP/good.log"; } > "$TMP/ffma9.log"
172check "a ninth FFMA launch refused" '! num_check_log "$TMP/ffma9.log"'
173{ cat "$TMP/good.log"; grep '"op":"FADD","tier":"gb10"' "$TMP/good.log"; } > "$TMP/fadd2.log"
174check "a second FADD launch refused" '! num_check_log "$TMP/fadd2.log"'
175gb10 cbits '/"op":"FADD"/s/"n":4096/"c_bits":"0x3f800000","n":4096/'
176check "c_bits on a non-FFMA op refused" '! num_check_log "$TMP/cbits.log"'
177gb10 stray '/"op":"FADD"/s/"op":"FADD"/"op":"NOPE"/'
178check "parity line for an op outside the manifest refused" '! num_check_log "$TMP/stray.log"'
179sed '/OMEGA_NUMERIC_REGISTRY_JSON:{"op":"FADD"/s/"compare":"BIT_EXACT"/"compare":"SEED_BOUND"/' "$TMP/good.log" > "$TMP/reg1.log"
180check "registry comparison differing from the manifest refused" '! num_check_log "$TMP/reg1.log"'
181sed '/OMEGA_NUMERIC_REGISTRY_JSON:{"op":"FFMA"/s/"launches":8/"launches":1/' "$TMP/good.log" > "$TMP/reg2.log"
182check "registry launch count differing from the manifest refused" '! num_check_log "$TMP/reg2.log"'
183{ cat "$TMP/good.log"; echo 'OMEGA_NUMERIC_REGISTRY_JSON:{"op":"NOPE","encoded":true,"compare":"BIT_EXACT","launches":1}'; } > "$TMP/reg3.log"
184check "registry encoding an op the manifest does not list refused" '! num_check_log "$TMP/reg3.log"'
185# LDS_STS and REDUCE_SUM: one line each, full check, declared summation order.
186grep -v '"op":"LDS_STS","tier":"gb10"' "$TMP/good.log" > "$TMP/nolds.log"
187check "missing LDS_STS GB10 parity line refused" '! num_check_log "$TMP/nolds.log"'
188check " naming the op" '[ "$M19R_ERR" = "no GB10 parity line for LDS_STS" ]'
189gb10 ldsbit '/"op":"LDS_STS"/s/"mismatches":0/"mismatches":1/'
190check "one LDS_STS mismatch refused" '! num_check_log "$TMP/ldsbit.log"'
191gb10 redbit '/"op":"REDUCE_SUM"/s/"mismatches":0/"mismatches":1/'
192check "one REDUCE_SUM mismatch refused" '! num_check_log "$TMP/redbit.log"'
193gb10 redall '/"op":"REDUCE_SUM"/s/"checked":128/"checked":4096/'
194check "REDUCE_SUM claiming all 4096 lanes checked refused" '! num_check_log "$TMP/redall.log"'
195check " needs n/32 = 128 warp sums" '[ "$M19R_ERR" = "GB10 parity for REDUCE_SUM: checked 4096, need 128" ]'
196gb10 redorder '/"op":"REDUCE_SUM"/s/"reduction_order":"[A-Z0-9_]*"/"reduction_order":"SEQUENTIAL_LANE_0_TO_31"/'
197check "REDUCE_SUM with another summation order refused" '! num_check_log "$TMP/redorder.log"'
198check " naming both orders" '[[ $M19R_ERR == *"SEQUENTIAL_LANE_0_TO_31"*"$NUM_REDUCE_ORDER"* ]]'
199gb10 rednoorder '/"op":"REDUCE_SUM"/s/"reduction_order":"[A-Z0-9_]*",//'
200check "REDUCE_SUM without a declared order refused" '! num_check_log "$TMP/rednoorder.log"'
201gb10 faddorder '/"op":"FADD"/s/"n":4096/"reduction_order":"PAIRWISE_TREE_LANE_DELTA_16_8_4_2_1","n":4096/'
202check "a summation order on an op that does not reduce refused" '! num_check_log "$TMP/faddorder.log"'
203{ cat "$TMP/good.log"; grep '"op":"REDUCE_SUM","tier":"gb10"' "$TMP/good.log"; } > "$TMP/red2.log"
204check "a second REDUCE_SUM launch refused" '! num_check_log "$TMP/red2.log"'
205grep -v '^OMEGA_NUMERIC_RUN_JSON' "$TMP/good.log" > "$TMP/norun.log"
206check "log without the binary's run line refused" '! num_check_log "$TMP/norun.log"'
207sed 's/"binary_sha256":"[0-9a-f]*"/"binary_sha256":""/' "$TMP/good.log" > "$TMP/nobin.log"
208check "run line without a binary digest refused" '! num_check_log "$TMP/nobin.log"'
209
210echo "evidence directory (receipts never land in a candidate tree)"
211keep_evd=${NUM_EVIDENCE_DIR:-}
212M19R_PHYSICS=$phys
213NUM_EVIDENCE_DIR=
214check "default evidence directory accepted" 'num_check_evidence_dir "$HERE"'
215check " is ~/workspace/evidence-out/OMEGA-NUMERIC-0" '[ "$NUM_EVIDENCE_DIR" = "$(realpath -m "$HOME/workspace/evidence-out/OMEGA-NUMERIC-0")" ]'
216check " and lies outside this repository" '! num_under "$NUM_EVIDENCE_DIR" "$HERE"'
217NUM_EVIDENCE_DIR=$HERE/evidence/OMEGA-NUMERIC-0
218check "evidence directory inside this repository refused" '! num_check_evidence_dir "$HERE"'
219check " saying receipts must live outside the trees" '[[ $M19R_ERR == *"receipts must live outside the candidate trees" ]]'
220NUM_EVIDENCE_DIR=$omega/evidence/OMEGA-NUMERIC-0
221check "evidence directory inside the omega candidate refused" '! num_check_evidence_dir "$omega"'
222NUM_EVIDENCE_DIR=$omega
223check "the omega tree itself refused" '! num_check_evidence_dir "$omega"'
224NUM_EVIDENCE_DIR=$phys/receipts
225check "evidence directory inside the physics checkout refused" '! num_check_evidence_dir "$omega"'
226NUM_EVIDENCE_DIR=$TMP/evd/../omega/receipts
227check " also when reached through .." '! num_check_evidence_dir "$omega"'
228ln -s "$omega" "$TMP/omega-link"
229NUM_EVIDENCE_DIR=$TMP/omega-link/receipts
230check " also when reached through a symlink" '! num_check_evidence_dir "$omega"'
231NUM_EVIDENCE_DIR=$omega-sibling
232check "a sibling whose name only starts like the tree accepted" 'num_check_evidence_dir "$omega"'
233check "script refuses an in-tree --evidence-dir with exit 2, before any run" '"$HERE/tests/run_numeric_gates.sh" --omega-candidate x --physics-candidate y --evidence-dir "$HERE/evidence/OMEGA-NUMERIC-0-intree-probe" >/dev/null 2>&1; [ $? = 2 ] && [ ! -e "$HERE/evidence/OMEGA-NUMERIC-0-intree-probe" ]'
234NUM_EVIDENCE_DIR=$keep_evd
235
236cpu_bin=$HERE/build/test_omega_numeric_cpu
237if [ -x "$cpu_bin" ]; then
238 "$cpu_bin" > "$TMP/cpu.log" 2>&1
239 check "real CPU-only run log refused (no receipt without the chip)" '! num_check_log "$TMP/cpu.log"'
240 # Drift: the test binary's own registry and FFMA launches equal the manifest.
241 m19r_tagged_json "$TMP/cpu.log" OMEGA_NUMERIC_REGISTRY_JSON | jq -sc . > "$TMP/cpu.reg"
242 check "binary registry equals the shell manifest" '[ -z "$(jq -r --argjson man "$(num_manifest_json)" "$NUM_JQ_REGISTRY" "$TMP/cpu.reg")" ]'
243 check "binary FFMA c values equal the shell manifest" '[ "$(m19r_tagged_json "$TMP/cpu.log" OMEGA_NUMERIC_PARITY_JSON | jq -sc "[.[] | select(.op == \"FFMA\" and .tier == \"cpu\") | .c_bits] | sort")" = "$(num_manifest_json | jq -c ".ffma_c | sort")" ]'
244 check "binary corpus size equals the shell manifest" '[ "$(m19r_tagged_json "$TMP/cpu.log" OMEGA_NUMERIC_PARITY_JSON | jq -s "[.[] | select(.tier == \"cpu\" and .op == \"FADD\") | .n] | .[0]")" = "$NUM_CORPUS_N" ]'
245 check "binary REDUCE_SUM checked count and summation order equal the shell manifest" '[ "$(m19r_tagged_json "$TMP/cpu.log" OMEGA_NUMERIC_PARITY_JSON | jq -sc "[.[] | select(.tier == \"cpu\" and .op == \"REDUCE_SUM\") | [.checked, .reduction_order]] | .[0]")" = "$(num_manifest_json | jq -c "[.checked.REDUCE_SUM, .reduction_order.REDUCE_SUM]")" ]'
246else
247 bad "build/test_omega_numeric_cpu missing (run make test-numeric-cpu first)"
248fi
249
250echo "execution record: exit status, binary and log digests, run id"
251# A stand-in gate binary: prints STUB_LOG, then the same run line the real
252# binary prints (run id from OMEGA_NUMERIC_RUN_ID, digest of /proc/self/exe),
253# then exits with STUB_RC. It also writes a line to stderr after every stdout
254# line, as the real binary's refusal messages do. It proves the plumbing,
255# never a chip result.
256cat > "$TMP/stub.c" <<'STUB'
257#include <stdio.h>
258#include <stdlib.h>
259#include "sha256.h"
260int main(void) {
261 const char *log = getenv("STUB_LOG"), *rc = getenv("STUB_RC"), *id = getenv("OMEGA_NUMERIC_RUN_ID");
262 char line[4096], hex[65] = "";
263 uint8_t buf[65536], dig[SHA256_DIGEST_SIZE];
264 size_t got;
265 sha256_ctx ctx;
266 FILE *f = log ? fopen(log, "r") : NULL;
267 if (f) { while (fgets(line, sizeof line, f)) { fputs(line, stdout); fputs("stub stderr chatter\n", stderr); } fclose(f); }
268 f = fopen("/proc/self/exe", "rb");
269 if (f) {
270 sha256_init(&ctx);
271 while ((got = fread(buf, 1, sizeof buf, f)) > 0) sha256_update(&ctx, buf, got);
272 sha256_final(&ctx, dig);
273 for (int i = 0; i < SHA256_DIGEST_SIZE; i++) snprintf(hex + 2 * i, 3, "%02x", dig[i]);
274 fclose(f);
275 }
276 printf("OMEGA_NUMERIC_RUN_JSON:{\"run_id\":\"%s\",\"binary_sha256\":\"%s\"}\n", id ? id : "", hex);
277 return rc ? atoi(rc) : 0;
278}
279STUB
280grep -v '^OMEGA_NUMERIC_RUN_JSON' "$TMP/good.log" > "$TMP/stub_body.log"
281mkdir -p "$omega/evidence"
282echo '{"historical":true}' > "$omega/evidence/m19r_gate5_omega_numeric_evidence.json"
283gitc "$omega" add -A && gitc "$omega" commit -qm "historical evidence"
284NUM_OMEGA_CAND=$(git -C "$omega" rev-parse HEAD) NUM_PHYSICS_CAND=$new M19R_PHYSICS=$phys
285NUM_TS=$(date -u +%Y-%m-%dT%H:%M:%S.%6NZ) NUM_RECORD=0
286EVD=$TMP/evidence-out/OMEGA-NUMERIC-0 NUM_EVIDENCE_DIR=$TMP/evidence-out/OMEGA-NUMERIC-0
287export STUB_LOG=$TMP/stub_body.log
288# fresh_run RC -- a new run directory, build the stand-in, run it like num_qualify.
289fresh_run() {
290 NUM_RUN_ID=unit-$1-$RANDOM NUM_RUN_DIR=$TMP/run-$NUM_RUN_ID
291 mkdir -p "$NUM_RUN_DIR"
292 gcc -std=gnu11 -O2 -I"$HERE/src" -o "$NUM_RUN_DIR/test_omega_numeric" "$TMP/stub.c" "$HERE/src/omega_om_units.c" || return 1
293 NUM_BINARY_SHA=$(m19r_sha_file "$NUM_RUN_DIR/test_omega_numeric")
294 NUM_RUN_COMMIT=$NUM_OMEGA_CAND
295 STUB_RC=$1 num_execute "$omega" "$NUM_RUN_DIR/test_omega_numeric"
296}
297# Finding 7: a nonzero exit status disqualifies, whatever the log says.
298check "binary printing only passing records but exiting 3 refused" '! fresh_run 3'
299check " exit status recorded" '[ "$NUM_EXEC_RC" = 3 ] && [ "$(cat "$NUM_RUN_DIR/gate5.status")" = 3 ]'
300check " its log passes every log check on its own" 'num_check_log "$NUM_RUN_DIR/gate5.log"'
301check " yet no receipt from it" '! num_receipt "$omega" "$NUM_RUN_DIR/gate5.log" && [ ! -e "$NUM_RUN_DIR/receipt-preview.json" ]'
302NUM_EXEC_RC=0
303check " nor with the exit status variable forged to 0 (status file says 3)" '! num_receipt "$omega" "$NUM_RUN_DIR/gate5.log"'
304
305check "binary exiting 0 accepted by the executor" 'fresh_run 0'
306check " stderr chatter kept out of the gate log" 'num_check_log "$NUM_RUN_DIR/gate5.log" && ! grep -q chatter "$NUM_RUN_DIR/gate5.log" && grep -q chatter "$NUM_RUN_DIR/gate5.stderr" && [ "$NUM_STDERR_SHA" = "$(m19r_sha_file "$NUM_RUN_DIR/gate5.stderr")" ]'
307check "passing run makes a PASS preview" 'num_receipt "$omega" "$NUM_RUN_DIR/gate5.log" && [ -f "$NUM_RUN_DIR/receipt-preview.json" ]'
308pv=$NUM_RUN_DIR/receipt-preview.json
309check " status PASS, exit status 0, digests bound" '[ "$(jq -r "(.status == \"PASS\") and (.gate_stderr_sha256 == \"$NUM_STDERR_SHA\") and (.digest_meaning | startswith(\"integrity only, not authenticity\")) and (.gate_binary_exit_status == 0) and (.candidate_binary_sha256 == \"$NUM_BINARY_SHA\") and (.gate_log_sha256 == \"$NUM_LOG_SHA\") and (.run_id == \"$NUM_RUN_ID\")" "$pv")" = true ]'
310check " clean flags from git status, commits from git" '[ "$(jq -r "(.candidate_trees_clean == {\"omega\":true,\"physics\":true}) and (.run_git_commit == \"$NUM_OMEGA_CAND\") and (.candidate_git_commit == .run_git_commit)" "$pv")" = true ]'
311check " observed counts 26/26/0, historical untouched, predecessor recorded" '[ "$(jq -c "[.observed_test_count,.observed_pass_count,.observed_fail_count]" "$pv")" = "[26,26,0]" ] && [ "$(cat "$omega/evidence/m19r_gate5_omega_numeric_evidence.json")" = "{\"historical\":true}" ] && [ "$(jq -r ".predecessor_historical_gate5_sha256 | length" "$pv")" = 64 ]'
312check " no permanent receipt without --record" '[ ! -e "$EVD" ] && [ ! -e "$omega/evidence/OMEGA-NUMERIC-0" ]'
313# Finding 4: nothing is taken on trust at receipt time.
314cp "$TMP/good.log" "$TMP/fabricated.log"
315check "fabricated log outside the run refused" '! num_receipt "$omega" "$TMP/fabricated.log"'
316echo x >> "$NUM_RUN_DIR/gate5.stderr"
317check "stderr changed after the run refused" '! num_receipt "$omega" "$NUM_RUN_DIR/gate5.log" && [[ $M19R_ERR == "gate stderr changed since the run"* ]]'
318sed -i '$d' "$NUM_RUN_DIR/gate5.stderr"
319cp "$NUM_RUN_DIR/gate5.log" "$TMP/keep.log"
320cp "$TMP/good.log" "$NUM_RUN_DIR/gate5.log"
321check "log replaced after the run refused" '! num_receipt "$omega" "$NUM_RUN_DIR/gate5.log"'
322check " as changed since the run" '[[ $M19R_ERR == "gate log changed since the run"* ]]'
323cp "$TMP/keep.log" "$NUM_RUN_DIR/gate5.log"
324keep_sha=$NUM_BINARY_SHA
325NUM_BINARY_SHA=$(printf 'other' | sha256sum | cut -d' ' -f1)
326check "64-hex binary digest not matching the built binary refused" '! num_receipt "$omega" "$NUM_RUN_DIR/gate5.log"'
327NUM_BINARY_SHA=$keep_sha
328cp "$NUM_RUN_DIR/test_omega_numeric" "$TMP/keep.bin"
329printf 'x' >> "$NUM_RUN_DIR/test_omega_numeric"
330check "binary changed after the run refused" '! num_receipt "$omega" "$NUM_RUN_DIR/gate5.log"'
331cp "$TMP/keep.bin" "$NUM_RUN_DIR/test_omega_numeric"
332# A log whose run line names another binary, with every stored digest made to agree.
333sed "s/\"binary_sha256\":\"[0-9a-f]*\"/\"binary_sha256\":\"$LOG_BIN_SHA\"/" "$TMP/keep.log" > "$NUM_RUN_DIR/gate5.log"
334keep_log_sha=$NUM_LOG_SHA NUM_LOG_SHA=$(m19r_sha_file "$NUM_RUN_DIR/gate5.log")
335check "log produced by a different binary refused" '! num_receipt "$omega" "$NUM_RUN_DIR/gate5.log"'
336check " naming both digests" '[ "$M19R_ERR" = "log was produced by binary $LOG_BIN_SHA, not $NUM_BINARY_SHA" ]'
337cp "$TMP/keep.log" "$NUM_RUN_DIR/gate5.log"; NUM_LOG_SHA=$keep_log_sha
338keep_id=$NUM_RUN_ID NUM_RUN_ID=unit-other-run
339mv "$NUM_RUN_DIR" "$TMP/run-$NUM_RUN_ID"; keep_dir=$NUM_RUN_DIR NUM_RUN_DIR=$TMP/run-$NUM_RUN_ID
340check "log from another run id refused" '! num_receipt "$omega" "$NUM_RUN_DIR/gate5.log"'
341mv "$NUM_RUN_DIR" "$keep_dir"; NUM_RUN_DIR=$keep_dir NUM_RUN_ID=$keep_id
342NUM_RUN_COMMIT=0000000000000000000000000000000000000000
343check "run commit differing from candidate refused" '! num_receipt "$omega" "$NUM_RUN_DIR/gate5.log"'
344NUM_RUN_COMMIT=$NUM_OMEGA_CAND
345keep_cand=$NUM_OMEGA_CAND NUM_OMEGA_CAND=$(printf '%040d' 1)
346check "omega candidate not at HEAD refused" '! num_receipt "$omega" "$NUM_RUN_DIR/gate5.log"'
347NUM_OMEGA_CAND=$keep_cand
348echo dirty > "$omega/untracked"
349check "dirty omega tree at receipt time refused" '! num_receipt "$omega" "$NUM_RUN_DIR/gate5.log"'
350rm -f "$omega/untracked"
351echo dirty >> "$phys/README"
352check "dirty physics tree at receipt time refused" '! num_receipt "$omega" "$NUM_RUN_DIR/gate5.log"'
353gitc "$phys" checkout -q README
354check "num_tree_clean reads real git status" '[ "$(num_tree_clean "$omega")" = true ] && echo x > "$omega/untracked" && [ "$(num_tree_clean "$omega")" = false ] && rm -f "$omega/untracked"'
355check "recheck passes again once restored" 'num_receipt "$omega" "$NUM_RUN_DIR/gate5.log"'
356NUM_RECORD=1
357check "PASS receipt into a repository other than this script's refused" '! num_receipt "$omega" "$NUM_RUN_DIR/gate5.log"'
358check " and nothing written" '[ ! -e "$EVD" ] && [ ! -e "$omega/evidence/OMEGA-NUMERIC-0" ]'
359keep_m19r=$M19R_OMEGA M19R_OMEGA=$omega
360check "PASS receipt recorded (script repository = this tree)" 'num_receipt "$omega" "$NUM_RUN_DIR/gate5.log" && [ -f "$EVD/$NUM_DIGEST.json" ] && [ "$NUM_PERMANENT" = "$(realpath -m "$EVD")/$NUM_DIGEST.json" ]'
361prec=$EVD/$NUM_DIGEST.json
362check " outside the tree, which stays clean" '[ "$(num_tree_clean "$omega")" = true ] && [ "$(num_tree_clean "$phys")" = true ] && [ ! -e "$omega/evidence/OMEGA-NUMERIC-0" ]'
363check " binds run id, both commits, clean flags and binary digest" '[ "$(jq -r "(.status == \"PASS\") and (.run_id == \"$NUM_RUN_ID\") and (.candidate_git_commit == \"$NUM_OMEGA_CAND\") and (.physics_candidate_git_commit == \"$NUM_PHYSICS_CAND\") and (.candidate_trees_clean == {\"omega\":true,\"physics\":true}) and (.candidate_binary_sha256 == \"$NUM_BINARY_SHA\")" "$prec")" = true ]'
364check " named by its digest, mode 0444" '[ "$(jq "del(.receipt_digest)" "$prec" | "$JSON_CANON" --sha256)" = "$NUM_DIGEST" ] && [ "$(stat -c %a "$prec")" = 444 ]'
365check " a second record of the same PASS refused" '! num_receipt "$omega" "$NUM_RUN_DIR/gate5.log" && [ "$(ls "$EVD"/*.json | wc -l)" = 1 ]'
366NUM_EVIDENCE_DIR=$omega/evidence/OMEGA-NUMERIC-0
367check "PASS receipt into an in-tree evidence directory refused" '! num_receipt "$omega" "$NUM_RUN_DIR/gate5.log" && [ ! -e "$omega/evidence/OMEGA-NUMERIC-0" ]'
368# Review finding: blobs/ planted as a symlink into the candidate tree. The
369# evidence directory itself is outside; only blobs/ points in.
370EVS=$TMP/evidence-symlinked; mkdir -p "$EVS" "$omega/planted"; ln -s "$omega/planted" "$EVS/blobs"
371NUM_EVIDENCE_DIR=$EVS
372check "blobs/ symlinked into the omega tree refused" '! num_receipt "$omega" "$NUM_RUN_DIR/gate5.log" && [[ $M19R_ERR == "blob directory "*"inside"* ]]'
373check " nothing written through the link, no receipt" '[ -z "$(ls -A "$omega/planted")" ] && ! ls "$EVS"/*.json >/dev/null 2>&1'
374rm -rf "$EVS" "$omega/planted"
375NUM_EVIDENCE_DIR=$EVD M19R_OMEGA=$keep_m19r
376rm -f "$prec" 2>/dev/null || { chmod u+w "$EVD"; rm -f "$prec"; }
377
378echo "append-only receipts"
379NUM_EXEC_RC=3
380# No git exclude needed: receipts live outside the tree, so it stays clean
381# and a repeat receipt is byte-identical.
382check "failed run writes a FAIL receipt" 'num_fail_receipt "$omega" "gate binary exited with status 3"'
383frec=$EVD/$NUM_DIGEST.json
384check " named by its digest, mode 0444" '[ -f "$frec" ] && [ "$(jq "del(.receipt_digest)" "$frec" | "$JSON_CANON" --sha256)" = "$NUM_DIGEST" ] && [ "$(stat -c %a "$frec")" = 444 ]'
385check " omega tree still clean after recording (reported true in the receipt)" '[ "$(num_tree_clean "$omega")" = true ] && [ "$(jq -r .candidate_trees_clean.omega "$frec")" = true ] && [ "$(jq -r ".run_id == \"$NUM_RUN_ID\"" "$frec")" = true ]'
386check " status FAIL with the reason and exit status" '[ "$(jq -r "(.status == \"FAIL\") and (.error == \"gate binary exited with status 3\") and (.gate_binary_exit_status == 3)" "$frec")" = true ]'
387check " blobs kept beside it, named by hash, mode 0444" '( for p in "log:$(jq -r .gate_log_sha256 "$frec")" "stderr:$(jq -r .gate_stderr_sha256 "$frec")" "bin:$(jq -r .candidate_binary_sha256 "$frec")"; do b=$EVD/blobs/${p#*:}.${p%%:*}; [ -f "$b" ] && [ "$(m19r_sha_file "$b")" = "${p#*:}" ] && [ "$(stat -c %a "$b")" = 444 ] || exit 1; done )'
388check " the blobs are this run's files" 'cmp -s "$EVD/blobs/$(jq -r .gate_log_sha256 "$frec").log" "$NUM_RUN_DIR/gate5.log" && cmp -s "$EVD/blobs/$(jq -r .candidate_binary_sha256 "$frec").bin" "$NUM_RUN_DIR/test_omega_numeric"'
389orig=$(sha256sum < "$frec")
390check "same FAIL receipt again refused (never overwritten)" '! num_fail_receipt "$omega" "gate binary exited with status 3"'
391check " because the file exists" '[[ $M19R_ERR == *"$NUM_DIGEST"* ]]'
392check " original bytes unchanged" '[ "$(sha256sum < "$frec")" = "$orig" ]'
393check "a different failure appends a second receipt" 'num_fail_receipt "$omega" "another reason" && [ "$(ls "$EVD"/*.json | wc -l)" = 2 ] && [ "$(num_tree_clean "$omega")" = true ]'
394NUM_EXEC_RC=0
395check "PASS preview present before a late failure" 'rm -f "$NUM_RUN_DIR/receipt-preview.json" && NUM_RECORD=0 num_receipt "$omega" "$NUM_RUN_DIR/gate5.log" && [ "$(jq -r .status "$NUM_RUN_DIR/receipt-preview.json")" = PASS ]'
396check "failure path (num_main) removes the PASS preview and records FAIL" 'num_on_failure "$omega" "late failure" 2>/dev/null && [ ! -e "$NUM_RUN_DIR/receipt-preview.json" ] && [ "$(jq -r "(.status == \"FAIL\") and (.error == \"late failure\")" "$NUM_PERMANENT")" = true ]'
397urec=$TMP/immut/unit.json
398check "immutable writer: first write" 'printf "{\"unit_test\":true,\"status\":\"PASS\"}" | m19r_write_immutable_receipt "$urec"'
399check "immutable writer: overwrite refused, bytes kept" '! printf "{\"unit_test\":true,\"status\":\"FAIL\"}" | m19r_write_immutable_receipt "$urec" && [ "$(jq -r .status "$urec")" = PASS ]'
400
401echo "GPU lock (flock) failure stops the run before any build or device access"
402# qual_probe FLOCK_RC -- run num_qualify with flock, build and execute stubbed;
403# the stubs leave marker files so the test sees how far the run got.
404qual_probe() {
405 rm -f "$TMP/probe.built" "$TMP/probe.ran"
406 (
407 M19R_OMEGA=$omega NUM_RUN_ID=unit-flock NUM_RUN_DIR=$TMP/run-flock
408 mkdir -p "$NUM_RUN_DIR"
409 flock() { return "$FLOCK_RC"; }
410 eval "real_m19r_git() $(declare -f m19r_git | tail -n +2)"
411 m19r_git() { [ -z "${GIT_BROKEN:-}" ] || return 1; real_m19r_git "$@"; }
412 num_build() { touch "$TMP/probe.built"; [ -n "${PROBE_BUILD_OK:-}" ] || return 1; : > "$3"; [ -n "${GIT_OK:-}" ] || GIT_BROKEN=1; }
413 num_execute() { touch "$TMP/probe.ran"; return 1; }
414 FLOCK_RC=$1 num_qualify
415 )
416}
417check "control: with the lock taken, the run reaches the build" '! qual_probe 0 && [ -e "$TMP/probe.built" ] && [ ! -e "$TMP/probe.ran" ]'
418check "lock failure: run refused, nothing built, nothing run on the device" '! qual_probe 1 && [ ! -e "$TMP/probe.built" ] && [ ! -e "$TMP/probe.ran" ]'
419check "HEAD unreadable after the build: run refused before the device" '! PROBE_BUILD_OK=1 qual_probe 0 && [ -e "$TMP/probe.built" ] && [ ! -e "$TMP/probe.ran" ]'
420check " control: with HEAD readable the run reaches the device" '! PROBE_BUILD_OK=1 GIT_OK=1 qual_probe 0 && [ -e "$TMP/probe.ran" ]'
421
422if [ "$fails" -ne 0 ]; then
423 echo "Gate 5 qualifier tests: $fails FAILED"
424 exit 1
425fi
426echo "Gate 5 qualifier tests: all passed"