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 / tests / run_numeric_gates.sh

623 lines · 35439 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# tests/run_numeric_gates.sh -- M19R Gate 5 (OMEGA-NUMERIC-0) qualification.
18#
19# tests/run_numeric_gates.sh --omega-candidate SHA --physics-candidate SHA
20# [--physics-dir DIR] [--evidence-dir DIR] [--record]
21#
22# Needs the GB10: the test binary launches kernels on the chip. The build and
23# the run happen under /tmp/aien-gb10.lock.
24#
25# Checks, in order:
26# 1. both candidates are explicit full 40-hex SHAs equal to HEAD of a clean
27# tree (omega = this repo, physics = --physics-dir, default ../physics);
28# 2. physics.lock names the physics candidate, and the physics checkout is at
29# that commit (the dependency comes from the pin file, never a fixed path);
30# 3. the pinned physics commit has the forge hardware probe (forge/); if not
31# the run stops and says so (moving the pin is an owner decision);
32# 4. clean build into build/qual-runs/<run-id>/ with -ffp-contract=off, no
33# libm and no CUDA symbols in the binary;
34# 5. the binary runs with OMEGA_NUMERIC_RUN_ID set; a nonzero exit status
35# fails the run even when every printed record passes;
36# 6. the log: every expected test ID printed exactly once as PASS, no SKIP;
37# one run line whose run id is this run and whose binary digest (the
38# binary hashes /proc/self/exe) is the file that was built; registry
39# lines equal to the manifest below; for every encoded op exactly the
40# expected GB10 parity lines (FFMA: one per uniform c in NUM_FFMA_C, the
41# others one) with the comparison mode taken from the manifest, never
42# from the log, full corpus size, typed integer counts and zero
43# mismatches; a hardware descriptor probed from the device (a fake one
44# is refused);
45# 7. receipt time: binary and log digests unchanged since the run, both
46# candidates re-checked, run commit re-read from git, evidence/
47# unchanged.
48#
49# Output: build/qual-runs/<run-id>/{build.log,gate5.log,gate5.stderr,gate5.status,run.json}
50# always; receipt-preview.json when every check passed. With --record a
51# permanent receipt <evidence-dir>/<receipt_digest>.json is written
52# for every run that got past argument parsing: "status":"PASS" when every
53# check passed, "status":"FAIL" (with the reason) otherwise. Receipts are
54# append-only: created exclusively, mode 0444, never overwritten. A PASS
55# receipt is written only for the repository this script belongs to. The
56# evidence directory defaults to ~/workspace/evidence-out/OMEGA-NUMERIC-0 and
57# is refused (exit 2) when it lies inside the omega tree or the physics
58# checkout, so recording a receipt never dirties a candidate tree; run.json
59# records whether both trees were still clean afterwards. Each receipt binds
60# the run id, the omega and physics commits with their clean flags, and the
61# gate binary digest.
62# The receipt digest shows the receipt was not altered after it was written;
63# it is not a signature and does not prove who wrote it. With --record the
64# gate log, stderr and binary it names are copied beside it as
65# <evidence-dir>/blobs/<sha256>.{log,stderr,bin} (mode 0444) so anyone can
66# re-hash them and re-run the log checks.
67# The historical file evidence/m19r_gate5_omega_numeric_evidence.json is not
68# touched; its hash is recorded as the predecessor.
69# All 42 ops in the manifest (the 15 original ops, the 23 E1 scalar ops,
70# DIV and SQRT, which run the E1 row 7 whole-program kernels, and EXP and
71# LOG, which run the E1 row 10 whole-program kernels),
72# LDS_STS and REDUCE_SUM included, need one GB10
73# parity line each (FFMA one per c) at n=4096 with zero mismatches against the
74# CPU reference. REDUCE_SUM checks lane 0 of each warp (n/32 sums) and must
75# name the declared summation order. A PASS needs a real chip run; nothing
76# here can produce one without the GB10.
77# Exit 0 on PASS, 1 on failure, 2 on bad arguments.
78#
79# Functions can be sourced (tools/test_numeric_qualify.sh does); main only
80# runs when the file is executed. Shell + coreutils + git + jq + gcc. No Python.
81
82NUM_SELF=$(cd -P "$(dirname "${BASH_SOURCE[0]}")" && pwd)/$(basename "${BASH_SOURCE[0]}")
83# shellcheck source=../tools/m19r_qualify.sh
84. "$(dirname "$NUM_SELF")/../tools/m19r_qualify.sh"
85M19R_PROG=run_numeric_gates.sh
86
87NUM_SUITE=gate5
88NUM_HISTORICAL=evidence/m19r_gate5_omega_numeric_evidence.json
89# Permanent receipts go here, outside every candidate tree (--evidence-dir).
90NUM_EVIDENCE_DEFAULT=$HOME/workspace/evidence-out/OMEGA-NUMERIC-0
91# Test IDs the chip build must print, each exactly once, each PASS.
92NUM_EXPECTED_IDS="FPCR_RNE_NO_FTZ_REQUIRED PROVENANCE_MATCHES_EXECUTOR
93NOT_ENCODED_OPS_REFUSED_BEFORE_SUBMISSION FP32_SIMT_OPCODES_ENCODED
94OMEGA_MATH_SEQUENCES_QUALIFIED WARP_REDUCTION_ORDER_DECLARED
95CPU_TIER_EQUALS_REFERENCE CPU_TIER_SUBNORMALS_PRESERVED CPU_TIER_INDEPENDENT_ORACLE
96CPU_GB10_BIT_PARITY
97SUBNORMALS_PRESERVED_NO_FTZ MUFU_SEED_ONLY_NOT_COMPARED
98EDGE_CLASS_BEHAVIOR_VERIFIED HARDWARE_DESCRIPTOR_PROBED
99NEG_FTZ_DETECTED_AND_REJECTED NEG_UNORDERED_REDUCTION_DIVERGENCE_CAUGHT
100NEG_RAW_MUFU_APPROX_REJECTED_WITHOUT_REFINEMENT NEG_UNKNOWN_OPCODE_FAILS_CLOSED
101NEG_OPCODE_PROVENANCE_INTEGRITY_VERIFIED NEG_NONDEFAULT_FPCR_REFUSED
102NEG_COMPARATOR_CATCHES_ONE_BIT NEG_BAD_SHARED_AND_WARP_SHAPES_REFUSED
103NEG_PATCH_STRUCTURE_CHECKED_BEFORE_SUBMISSION
104E1_SCALAR_BOUNDARY_VALUES E1_SCALAR_CPU_EQUALS_REFERENCE E1_SCALAR_INDEPENDENT_ORACLE"
105# The operation manifest: every op the executor encodes for GB10 and how its
106# result is compared. It mirrors OP_TABLE in src/omega_numeric.c (the host
107# test checks the two agree). The log never chooses its own comparison.
108NUM_OP_MANIFEST="FADD:BIT_EXACT FSUB:BIT_EXACT FMUL:BIT_EXACT FFMA:BIT_EXACT
109FSETP_SEL:INT_EXACT FSEL:INT_EXACT FMNMX_MIN:BIT_EXACT FMNMX_MAX:BIT_EXACT
110I2FP:BIT_EXACT F2I:INT_EXACT MUFU_RCP:SEED_BOUND MUFU_RSQ:SEED_BOUND
111SHFL_DOWN:INT_EXACT LDS_STS:INT_EXACT REDUCE_SUM:BIT_EXACT
112FSETP_LT_SEL:INT_EXACT FSETP_LE_SEL:INT_EXACT FSETP_GT_SEL:INT_EXACT FSETP_EQ_SEL:INT_EXACT
113FSETP_NE_SEL:INT_EXACT FSETP_NUM_SEL:INT_EXACT FSETP_NAN_SEL:INT_EXACT FSETP_LTU_SEL:INT_EXACT
114FSETP_LEU_SEL:INT_EXACT FSETP_GTU_SEL:INT_EXACT FSETP_GEU_SEL:INT_EXACT FSETP_EQU_SEL:INT_EXACT
115FSETP_NEU_SEL:INT_EXACT F2I_FLOOR:INT_EXACT F2I_CEIL:INT_EXACT F2I_RNI:INT_EXACT F2U:INT_EXACT
116I2FP_U32:BIT_EXACT F32_TO_F16:F16_BITS F32_TO_BF16:BF16_BITS F16_TO_F32:BIT_EXACT
117BF16_TO_F32:BIT_EXACT FFMA_V:BIT_EXACT DIV:BIT_EXACT SQRT:BIT_EXACT EXP:BIT_EXACT LOG:BIT_EXACT"
118NUM_ENCODED_OPS=$(printf '%s\n' $NUM_OP_MANIFEST | cut -d: -f1 | tr '\n' ' ')
119# Elements per launch (N in tests/test_omega_numeric.c).
120NUM_CORPUS_N=4096
121# FFMA runs once per uniform c (FFMA_C in tests/test_omega_numeric.c).
122NUM_FFMA_C="0x3f800000 0x80000000 0x00800000 0xbf800000 0x7fc00000 0x7f800000
1230x00000003 0x80800000"
124# REDUCE_SUM: only lane 0 of each 32-lane warp holds a sum, so it checks N/32
125# elements, and its parity line must name this summation order (the order
126# the chip kernel and the CPU reference both use: SHFL.DOWN deltas 16, 8, 4,
127# 2, 1, one FADD after each; OMEGA_WARP_REDUCTION_DECLARED_ORDER).
128NUM_REDUCE_ORDER=PAIRWISE_TREE_LANE_DELTA_16_8_4_2_1
129
130# num_manifest_json -- {"ops":{op:mode},"n":N,"ffma_c":[...],
131# "checked":{op:count} (ops that do not check all N), "reduction_order":{op:order}}
132num_manifest_json() {
133 jq -n --arg m "$NUM_OP_MANIFEST" --arg c "$NUM_FFMA_C" --argjson n "$NUM_CORPUS_N" --arg ro "$NUM_REDUCE_ORDER" '
134 {ops: ([$m | splits("\\s+") | select(length > 0) | split(":") | {key: .[0], value: .[1]}]
135 | from_entries),
136 n: $n, ffma_c: [$c | splits("\\s+") | select(length > 0)],
137 checked: {REDUCE_SUM: ($n / 32 | floor)}, reduction_order: {REDUCE_SUM: $ro}}'
138}
139
140# num_check_physics OMEGA PHYSICS_DIR PHYSICS_CAND -- the physics dependency
141# is the commit in physics.lock; the checkout must be at it and carry forge/.
142num_check_physics() {
143 local omega=$1 physics=$2 cand=$3 locked head f
144 [ -r "$omega/physics.lock" ] || m19r_fail "cannot read $omega/physics.lock" || return 1
145 locked=$(m19r_strip "$(cat "$omega/physics.lock")")
146 [[ $locked =~ ^[0-9a-fA-F]{40}$ ]] ||
147 m19r_fail "physics.lock does not hold a full 40-hex commit" || return 1
148 [ "${locked,,}" = "${cand,,}" ] ||
149 m19r_fail "physics.lock pins $locked but --physics-candidate is $cand" || return 1
150 [ -d "$physics" ] ||
151 m19r_fail "physics checkout $physics does not exist (pinned commit $locked)" || return 1
152 m19r_git "$physics" rev-parse HEAD ||
153 m19r_fail "physics checkout $physics is not a git repository" || return 1
154 head=$M19R_GIT
155 [ "${head,,}" = "${locked,,}" ] ||
156 m19r_fail "physics checkout $physics is at $head but physics.lock pins $locked" || return 1
157 m19r_must_candidate "$physics" "$cand" || return 1
158 for f in forge/forge_descriptor.h forge/forge_descriptor.c forge/forge_realize.c \
159 sha256_clean.c nvrm/nvrm.c m16/m16_native.c; do
160 [ -f "$physics/$f" ] || m19r_fail "pinned physics commit $locked has no $f; Gate 5 needs the forge hardware probe, so physics.lock must move to a commit that has it (owner decision, not done by this script)" || return 1
161 done
162}
163
164# num_build OMEGA PHYSICS OUT LOG -- build the chip test binary.
165num_build() {
166 local omega=$1 p=$2 out=$3 log=$4 nv
167 nv=$p/third_party/nvidia-open-580.173.02
168 (cd "$omega" && gcc -std=gnu11 -O2 -Wall -Wextra -Werror -ffp-contract=off \
169 -Isrc -I"$p/forge" -I"$p/nvrm" -I"$p/m16" \
170 -I"$nv/src/common/sdk/nvidia/inc" -I"$nv/kernel-open/common/inc" \
171 -I"$nv/kernel-open/nvidia-uvm" -I"$nv/src/nvidia/arch/nvalloc/unix/include" \
172 -o "$out" tests/test_omega_numeric.c src/omega_numeric.c src/omega_numeric_gb10.c \
173 src/omega_numeric_divsqrt_gb10.c \
174 src/omega_numeric_provenance.c src/omega_blackwell_codegen.c \
175 src/omega_blackwell_encoder.c src/omega_blackwell_matmul.c src/omega_blackwell_qmd.c \
176 src/forge_realization.c src/aegis_verification.c src/omega_om_units.c \
177 "$p/forge/forge_descriptor.c" "$p/forge/forge_realize.c" "$p/sha256_clean.c" \
178 "$p/nvrm/nvrm.c" "$p/m16/m16_native.c") > "$log" 2>&1 ||
179 m19r_fail "build failed; see $log" || return 1
180 if nm -u "$out" | grep -Eq '\b(sqrtf?|expf?|logf?|powf?|fmaf?|sinf?|cosf?|roundf?|fabsf?)\b'; then
181 m19r_fail "libm math symbols found in $out"; return 1
182 fi
183 if nm -u "$out" | grep -Eq "$M19R_CUDA_RE"; then
184 m19r_fail "CUDA symbols found in $out"; return 1
185 fi
186}
187
188# num_execute OMEGA BIN -- run the gate binary once for this run. Writes
189# $NUM_RUN_DIR/gate5.log and gate5.status, sets NUM_EXEC_RC and NUM_LOG_SHA.
190# A nonzero exit status fails, whatever the log says.
191num_execute() {
192 local omega=$1 bin=$2 rc
193 NUM_EXEC_RC= NUM_LOG_SHA= NUM_STDERR_SHA=
194 # stdout and stderr go to separate files: the binary's stderr (refusal
195 # messages) is unbuffered and would otherwise land inside half-written
196 # stdout JSON lines (seen on the chip run 20260930T164139Z-890338e1a62e).
197 (cd "$omega" && exec env OMEGA_NUMERIC_RUN_ID="$NUM_RUN_ID" "$bin" 9>&-) \
198 > "$NUM_RUN_DIR/gate5.log" 2> "$NUM_RUN_DIR/gate5.stderr"
199 rc=$?
200 NUM_EXEC_RC=$rc
201 printf '%s\n' "$rc" > "$NUM_RUN_DIR/gate5.status"
202 NUM_LOG_SHA=$(m19r_sha_file "$NUM_RUN_DIR/gate5.log")
203 NUM_STDERR_SHA=$(m19r_sha_file "$NUM_RUN_DIR/gate5.stderr")
204 [ "$rc" -eq 0 ] ||
205 m19r_fail "gate binary exited with status $rc; a crash or late failure disqualifies the run" || return 1
206}
207
208# jq: the first problem with the GB10 parity lines against the manifest, or "".
209# Input: array of GB10 parity lines. $man: num_manifest_json.
210NUM_JQ_PARITY='
211def isint: type == "number" and . == floor and . >= 0;
212def problem($op; $m):
213 . as $l
214 | ($man.checked[$op] // $man.n) as $need
215 | ($man.reduction_order[$op] // null) as $order
216 | if ($l | has("error")) then "a launch error (\($l.error | tojson))"
217 elif $l.reduction_order != $order then "reduction_order \($l.reduction_order | tojson), the manifest says \($order | tojson)"
218 elif ($l.compare != $m) then "comparison \($l.compare | tojson) but the manifest says \($m)"
219 elif (($l.n | isint) | not) or $l.n != $man.n then "n \($l.n | tojson), need \($man.n)"
220 elif (($l.checked | isint) | not) then "checked is not an integer"
221 elif $m == "SEED_BOUND" then
222 if (($l.out_of_bound | isint) | not) or (($l.skipped | isint) | not) then "seed-bound counts are not integers"
223 elif $l.checked == 0 then "no element checked"
224 elif $l.checked + $l.skipped != $l.n then "checked + skipped != n"
225 elif $l.out_of_bound != 0 then "\($l.out_of_bound) results out of bound"
226 else "" end
227 else
228 if $l.checked != $need then "checked \($l.checked), need \($need)"
229 elif (($l.mismatches | isint) | not) then "mismatches is not an integer"
230 elif $l.mismatches != 0 then "\($l.mismatches) mismatches"
231 else "" end
232 end;
233. as $all
234| ([$all[] | select(.tier != "gb10") | .op] | first) as $badtier
235| ([$all[] | .op as $o | select(($man.ops | has($o)) | not) | $o] | first) as $stray
236| if $badtier != null then "a parity line for \($badtier | tojson) is not tier gb10"
237 elif $stray != null then "a GB10 parity line names \($stray | tojson), outside the encoded set"
238 else
239 [ $man.ops | to_entries[] | .key as $op | .value as $mode
240 | [$all[] | select(.op == $op)] as $ls
241 | if ($ls | length) == 0 then "no GB10 parity line for \($op)"
242 elif $op == "FFMA" then
243 if ([$ls[] | .c_bits] | sort) != ($man.ffma_c | sort)
244 then "FFMA launches \([$ls[] | .c_bits] | tojson), need exactly one per c in \($man.ffma_c | tojson)"
245 else ([$ls[] | problem($op; $mode) | select(. != "") | "GB10 parity for FFMA: " + .] | first // "") end
246 elif ($ls | length) != 1 then "GB10 parity for \($op): \($ls | length) lines, need exactly 1"
247 elif ($ls[0] | has("c_bits")) then "GB10 parity for \($op) carries c_bits"
248 else ($ls[0] | problem($op; $mode) | if . == "" then "" else "GB10 parity for \($op): " + . end) end
249 | select(. != "") ] | first // ""
250 end'
251
252# jq: the first problem with the registry lines against the manifest, or "".
253NUM_JQ_REGISTRY='
254([.[] | select(.encoded == true)]) as $enc
255| if ([.[] | .op] | length) != ([.[] | .op] | unique | length) then "an op appears twice in the registry lines"
256 elif ([$enc[] | .op] | sort) != ($man.ops | keys | sort)
257 then "registry encodes \([$enc[] | .op] | sort | tojson), manifest lists \($man.ops | keys | sort | tojson)"
258 else ([$enc[] | select(.compare != $man.ops[.op]) | "registry compares \(.op) as \(.compare | tojson), manifest says \($man.ops[.op])"]
259 + [$enc[] | select(.launches != (if .op == "FFMA" then ($man.ffma_c | length) else 1 end))
260 | "registry has \(.launches | tojson) launches for \(.op)"]) | first // ""
261 end'
262
263# num_check_log LOG -- validate the gate log. Sets NUM_TSV (events file),
264# NUM_HWDESC, NUM_HWDIGEST, NUM_PARITY (JSON array of GB10 parity lines),
265# NUM_LOG_RUN_ID, NUM_LOG_BINARY_SHA (from the run line).
266num_check_log() {
267 local log=$1 id n hw man reg run problem
268 [ -r "$log" ] || m19r_fail "cannot read gate log $log" || return 1
269 if grep -Eq '^[[:space:]]*\[SKIP\]' "$log"; then
270 m19r_fail "gate log has SKIP results; a qualifying run executes every test"; return 1
271 fi
272 NUM_TSV=$M19R_TMP/gate5.tsv
273 m19r_events "$log" "$NUM_SUITE" > "$NUM_TSV"
274 for id in $NUM_EXPECTED_IDS; do
275 n=$(awk -F'\t' -v i="$id" '$2 == i' "$NUM_TSV" | grep -c .)
276 [ "$n" -eq 1 ] || m19r_fail "expected test $id printed $n times (need exactly 1)" || return 1
277 done
278 n=$(grep -c . "$NUM_TSV")
279 [ "$n" -eq "$(printf '%s\n' $NUM_EXPECTED_IDS | grep -c .)" ] ||
280 m19r_fail "gate log has $n results; the manifest lists $(printf '%s\n' $NUM_EXPECTED_IDS | grep -c .)" || return 1
281 m19r_require_passed < "$NUM_TSV" || return 1
282
283 run=$(m19r_tagged_json "$log" OMEGA_NUMERIC_RUN_JSON) || return 1
284 [ "$(printf '%s\n' "$run" | grep -c .)" -eq 1 ] ||
285 m19r_fail "expected exactly one run line (OMEGA_NUMERIC_RUN_JSON)" || return 1
286 NUM_LOG_RUN_ID=$(printf '%s' "$run" | jq -r 'if (.run_id | type) == "string" then .run_id else "" end')
287 NUM_LOG_BINARY_SHA=$(printf '%s' "$run" | jq -r 'if (.binary_sha256 | type) == "string" then .binary_sha256 else "" end')
288 [ -n "$NUM_LOG_RUN_ID" ] || m19r_fail "run line has no run id" || return 1
289 [[ $NUM_LOG_BINARY_SHA =~ ^[0-9a-f]{64}$ ]] ||
290 m19r_fail "run line has no binary digest" || return 1
291
292 hw=$(m19r_tagged_json "$log" OMEGA_NUMERIC_HWDESC_JSON) || return 1
293 [ "$(printf '%s\n' "$hw" | grep -c .)" -eq 1 ] ||
294 m19r_fail "expected exactly one hardware descriptor line" || return 1
295 [ "$(printf '%s' "$hw" | jq -r '.source')" = FORGE_PROBE ] ||
296 m19r_fail "hardware descriptor is not a device probe (source $(printf '%s' "$hw" | jq -c '.source')); refusing" || return 1
297 [ "$(printf '%s' "$hw" | jq -r '.fake // false')" = false ] ||
298 m19r_fail "hardware descriptor is marked fake; refusing" || return 1
299 NUM_HWDIGEST=$(printf '%s' "$hw" | jq -r '.descriptor_digest')
300 [[ $NUM_HWDIGEST =~ ^[0-9a-f]{64}$ ]] && [ "$NUM_HWDIGEST" != "$(printf '0%.0s' {1..64})" ] ||
301 m19r_fail "hardware descriptor digest is missing or all zero" || return 1
302 NUM_HWDESC=$hw
303
304 man=$(num_manifest_json) || m19r_fail "cannot build the operation manifest" || return 1
305 reg=$(m19r_tagged_json "$log" OMEGA_NUMERIC_REGISTRY_JSON | jq -sc .) ||
306 m19r_fail "cannot read registry lines" || return 1
307 problem=$(printf '%s' "$reg" | jq -r --argjson man "$man" "$NUM_JQ_REGISTRY") ||
308 m19r_fail "cannot check registry lines" || return 1
309 [ -z "$problem" ] || m19r_fail "$problem" || return 1
310
311 NUM_PARITY=$(m19r_tagged_json "$log" OMEGA_NUMERIC_PARITY_JSON | jq -sc '[.[] | select(.tier == "gb10")]') ||
312 m19r_fail "cannot read GB10 parity lines" || return 1
313 problem=$(printf '%s' "$NUM_PARITY" | jq -r --argjson man "$man" "$NUM_JQ_PARITY") ||
314 m19r_fail "cannot check GB10 parity lines" || return 1
315 [ -z "$problem" ] || m19r_fail "$problem" || return 1
316}
317
318# num_under PATH DIR -- true when PATH is DIR or inside it (both resolved).
319num_under() {
320 local p d
321 p=$(realpath -m "$1") && d=$(realpath -m "$2") || return 1
322 [ "$p" = "$d" ] || [[ $p == "$d"/* ]]
323}
324
325# num_check_evidence_dir OMEGA -- resolve NUM_EVIDENCE_DIR (default
326# $NUM_EVIDENCE_DEFAULT) and refuse it when it lies inside the omega tree,
327# this script's repository or the physics checkout: a receipt written there
328# would dirty the very tree it certifies as clean.
329num_check_evidence_dir() {
330 local omega=$1 d
331 d=$(realpath -m "${NUM_EVIDENCE_DIR:-$NUM_EVIDENCE_DEFAULT}") ||
332 m19r_fail "cannot resolve evidence directory ${NUM_EVIDENCE_DIR:-$NUM_EVIDENCE_DEFAULT}" || return 1
333 num_outside_trees "$d" "$omega" "evidence directory" || return 1
334 NUM_EVIDENCE_DIR=$d
335}
336
337# num_outside_trees PATH OMEGA WHAT -- refuse PATH (already resolved) when it
338# lies inside the omega tree, this script's repository or the physics checkout.
339num_outside_trees() {
340 local d=$1 omega=$2 what=$3 t
341 for t in "$omega" "${M19R_OMEGA:-}" "${M19R_PHYSICS:-}"; do
342 [ -n "$t" ] || continue
343 if num_under "$d" "$t"; then
344 m19r_fail "$what $d is inside $(realpath -m "$t"); receipts must live outside the candidate trees" || return 1
345 fi
346 done
347}
348
349
350# num_tree_clean REPO -- "true" or "false" from git status right now.
351num_tree_clean() {
352 if m19r_git "$1" status --porcelain --untracked-files=normal && [ -z "$M19R_GIT" ]; then
353 echo true
354 else
355 echo false
356 fi
357}
358
359# num_keep_blob FILE EXT OMEGA -- copy FILE into NUM_EVIDENCE_DIR/blobs/<sha256>.EXT
360# (mode 0444). An existing blob must already hash to that name. Receipts name
361# the gate log, stderr and binary by digest; the digest shows the receipt was
362# not altered, it does not prove who wrote it, so the named bytes are kept
363# beside it for anyone to re-hash and re-check.
364num_keep_blob() {
365 local src=$1 ext=$2 omega=$3 sha dst tmp bdir
366 [ -f "$src" ] || return 0
367 sha=$(m19r_sha_file "$src") && dst=$NUM_EVIDENCE_DIR/blobs/$sha.$ext
368 mkdir -p "$NUM_EVIDENCE_DIR/blobs" || m19r_fail "cannot create $NUM_EVIDENCE_DIR/blobs" || return 1
369 # blobs/ may be a symlink planted into a candidate tree: resolve it and
370 # check where it really points before anything is written through it.
371 bdir=$(realpath -e "$NUM_EVIDENCE_DIR/blobs") && [ -d "$bdir" ] ||
372 m19r_fail "cannot resolve $NUM_EVIDENCE_DIR/blobs" || return 1
373 num_outside_trees "$bdir" "$omega" "blob directory" || return 1
374 dst=$bdir/$sha.$ext
375 if [ -e "$dst" ]; then
376 [ "$(m19r_sha_file "$dst")" = "$sha" ] || m19r_fail "blob $dst does not hash to its name" || return 1
377 return 0
378 fi
379 tmp=$(mktemp "$bdir/.tmp.XXXXXX") || return 1
380 cp "$src" "$tmp" && chmod 0444 "$tmp" && [ "$(m19r_sha_file "$tmp")" = "$sha" ] &&
381 mv -n "$tmp" "$dst" && [ ! -e "$tmp" ] ||
382 { rm -f "$tmp"; m19r_fail "cannot keep blob $dst"; return 1; }
383}
384
385# num_write_receipt OMEGA BODY [PREVIEW=1] -- digest the body, write the
386# preview (PASS only), and with NUM_RECORD=1 the permanent append-only
387# receipt NUM_EVIDENCE_DIR/<digest>.json (never inside a candidate tree).
388# Sets NUM_DIGEST and NUM_PERMANENT.
389num_write_receipt() {
390 local omega=$1 body=$2 preview=${3:-1} digest receipt
391 # The digest makes alteration detectable; it is not a signature and does
392 # not prove who wrote the receipt. The bytes it names are kept in blobs/.
393 body="${body%\}},\"digest_meaning\":\"integrity only, not authenticity: re-hash blobs/<sha256>.{log,stderr,bin} named here\"}"
394 digest=$(printf '%s' "$body" | "$JSON_CANON" --sha256) ||
395 m19r_fail "receipt body is not valid JSON" || return 1
396 receipt="{\"receipt_digest\":\"$digest\",${body#\{}"
397 if [ "$preview" = 1 ] && [ -n "${NUM_RUN_DIR:-}" ] && [ -d "$NUM_RUN_DIR" ]; then
398 printf '%s' "$receipt" | "$JSON_CANON" --pretty > "$NUM_RUN_DIR/receipt-preview.json" ||
399 m19r_fail "cannot write receipt preview" || return 1
400 fi
401 NUM_DIGEST=$digest
402 if [ "${NUM_RECORD:-0}" = 1 ]; then
403 num_check_evidence_dir "$omega" || return 1
404 if [ -n "${NUM_RUN_DIR:-}" ]; then
405 num_keep_blob "$NUM_RUN_DIR/gate5.log" log "$omega" || return 1
406 num_keep_blob "$NUM_RUN_DIR/gate5.stderr" stderr "$omega" || return 1
407 num_keep_blob "$NUM_RUN_DIR/test_omega_numeric" bin "$omega" || return 1
408 fi
409 NUM_PERMANENT=$NUM_EVIDENCE_DIR/$digest.json
410 m19r_write_immutable_receipt "$NUM_PERMANENT" <<< "$receipt" || { NUM_PERMANENT=; return 1; }
411 fi
412}
413
414# num_receipt OMEGA LOG -- revalidate everything this run produced and write
415# the PASS receipt. Nothing supplied by the caller is taken on trust: the log
416# must be this run's gate5.log with the digest recorded when the binary
417# exited 0, the binary must still hash to NUM_BINARY_SHA and match the run
418# line, both candidates are re-checked and the run commit is re-read from git.
419# Needs NUM_OMEGA_CAND NUM_PHYSICS_CAND NUM_RUN_COMMIT NUM_BINARY_SHA
420# NUM_EXEC_RC NUM_LOG_SHA NUM_RUN_ID NUM_TS NUM_RUN_DIR M19R_PHYSICS.
421num_receipt() {
422 local omega=$1 log=$2 bin bin_sha head status manifest manifest_digest counts body pred
423 local omega_clean physics_clean
424 [ -n "${NUM_RUN_DIR:-}" ] && [ "$log" = "$NUM_RUN_DIR/gate5.log" ] ||
425 m19r_fail "log $log is not this run's gate5.log" || return 1
426 [ "${NUM_EXEC_RC:-}" = 0 ] ||
427 m19r_fail "gate binary exit status is ${NUM_EXEC_RC:-unknown}, need 0" || return 1
428 status=$(cat "$NUM_RUN_DIR/gate5.status" 2>/dev/null)
429 [ "$status" = 0 ] || m19r_fail "recorded gate binary status is '${status}', need 0" || return 1
430 [ -f "$log" ] && [ "$(m19r_sha_file "$log")" = "${NUM_LOG_SHA:-}" ] ||
431 m19r_fail "gate log changed since the run (or no run recorded its digest)" || return 1
432 [ -f "$NUM_RUN_DIR/gate5.stderr" ] && [ "$(m19r_sha_file "$NUM_RUN_DIR/gate5.stderr")" = "${NUM_STDERR_SHA:-}" ] ||
433 m19r_fail "gate stderr changed since the run (or no run recorded its digest)" || return 1
434 bin=$NUM_RUN_DIR/test_omega_numeric
435 [ -f "$bin" ] || m19r_fail "gate binary $bin is missing" || return 1
436 bin_sha=$(m19r_sha_file "$bin")
437 [ "$bin_sha" = "${NUM_BINARY_SHA:-}" ] ||
438 m19r_fail "gate binary digest $bin_sha differs from the built binary ${NUM_BINARY_SHA:-(none)}" || return 1
439 num_check_log "$log" || return 1
440 [ "$NUM_LOG_RUN_ID" = "$NUM_RUN_ID" ] ||
441 m19r_fail "log run id $NUM_LOG_RUN_ID is not this run ($NUM_RUN_ID)" || return 1
442 [ "$NUM_LOG_BINARY_SHA" = "$bin_sha" ] ||
443 m19r_fail "log was produced by binary $NUM_LOG_BINARY_SHA, not $bin_sha" || return 1
444 m19r_must_candidate "$omega" "$NUM_OMEGA_CAND" || return 1
445 num_check_physics "$omega" "$M19R_PHYSICS" "$NUM_PHYSICS_CAND" || return 1
446 m19r_git "$omega" rev-parse HEAD || return 1
447 head=$M19R_GIT
448 [ "${head,,}" = "${NUM_RUN_COMMIT,,}" ] && [ "${head,,}" = "${NUM_OMEGA_CAND,,}" ] ||
449 m19r_fail "run commit ${NUM_RUN_COMMIT} / HEAD $head differ from candidate $NUM_OMEGA_CAND" || return 1
450 omega_clean=$(num_tree_clean "$omega")
451 physics_clean=$(num_tree_clean "$M19R_PHYSICS")
452 [ "$omega_clean" = true ] && [ "$physics_clean" = true ] ||
453 m19r_fail "candidate trees not clean at receipt time" || return 1
454 [[ $NUM_TS =~ ^[0-9]{4}-[0-9]{2}-[0-9]{2}T[0-9:.]+Z$ ]] || m19r_fail "missing UTC timestamp" || return 1
455 if [ "${NUM_RECORD:-0}" = 1 ] && [ "$(cd -P "$omega" && pwd)" != "$M19R_OMEGA" ]; then
456 m19r_fail "a recorded receipt certifies only the checkout this script runs from ($M19R_OMEGA), not $omega" || return 1
457 fi
458 pred=null
459 [ ! -f "$omega/$NUM_HISTORICAL" ] || pred="\"$(m19r_sha_file "$omega/$NUM_HISTORICAL")\""
460 manifest="{\"command\":\"tests/test_omega_numeric\",\"expected_test_ids\":$(printf '%s\n' $NUM_EXPECTED_IDS | jq -Rn '[inputs]'),\"encoded_ops\":$(printf '%s\n' $NUM_ENCODED_OPS | jq -Rn '[inputs]'),\"operation_manifest\":$(num_manifest_json)}"
461 manifest_digest=$(printf '%s' "$manifest" | "$JSON_CANON" --sha256) || return 1
462 counts=$(m19r_observed_counts < "$NUM_TSV")
463 body="{\"schema\":\"AIEN_OMEGA_NUMERIC_0_V1\",\"gate\":\"M19R_GATE5_OMEGA_NUMERIC_0\",\"status\":\"PASS\""
464 body+=",\"run_id\":$(m19r_jstr "$NUM_RUN_ID"),\"timestamp_utc\":\"$NUM_TS\""
465 body+=",\"candidate_git_commit\":\"${NUM_OMEGA_CAND,,}\",\"run_git_commit\":\"${head,,}\""
466 body+=",\"physics_candidate_git_commit\":\"${NUM_PHYSICS_CAND,,}\""
467 body+=",\"physics_lock\":\"$(m19r_strip "$(cat "$omega/physics.lock")" | tr 'A-F' 'a-f')\""
468 body+=",\"candidate_trees_clean\":{\"omega\":$omega_clean,\"physics\":$physics_clean}"
469 body+=",\"candidate_binary_sha256\":\"$bin_sha\",\"gate_binary_exit_status\":0"
470 body+=",\"gate_log_sha256\":\"$NUM_LOG_SHA\""
471 body+=",\"gate_stderr_sha256\":\"$NUM_STDERR_SHA\""
472 body+=",\"test_manifest_sha256\":\"$manifest_digest\",\"test_manifest\":$manifest"
473 body+=",\"test_results\":$(m19r_events_json < "$NUM_TSV")"
474 body+=",\"observed_test_count\":$(jq -n --argjson c "$counts" '$c.completed')"
475 body+=",\"observed_pass_count\":$(jq -n --argjson c "$counts" '$c.passed')"
476 body+=",\"observed_fail_count\":$(jq -n --argjson c "$counts" '$c.failed')"
477 body+=",\"hardware_descriptor\":$NUM_HWDESC,\"hardware_descriptor_digest\":\"$NUM_HWDIGEST\""
478 body+=",\"gb10_parity\":$NUM_PARITY"
479 body+=",\"predecessor_historical_gate5_sha256\":$pred,\"zero_libm_zero_libcuda\":true}"
480 num_write_receipt "$omega" "$body"
481}
482
483# num_fail_receipt OMEGA REASON -- the FAIL receipt for a run that did not
484# qualify: what was observed, nothing claimed. Only with NUM_RECORD=1.
485num_fail_receipt() {
486 local omega=$1 reason=$2 body f v
487 body="{\"schema\":\"AIEN_OMEGA_NUMERIC_0_V1\",\"gate\":\"M19R_GATE5_OMEGA_NUMERIC_0\",\"status\":\"FAIL\""
488 body+=",\"error\":$(m19r_jstr "$reason"),\"run_id\":$(m19r_jstr "$NUM_RUN_ID")"
489 body+=",\"timestamp_utc\":$(m19r_jstr "${NUM_TS:-$(date -u +%Y-%m-%dT%H:%M:%S.%6NZ)}")"
490 body+=",\"supplied_omega_candidate\":$(m19r_jstr "$NUM_OMEGA_CAND")"
491 body+=",\"supplied_physics_candidate\":$(m19r_jstr "$NUM_PHYSICS_CAND")"
492 v=null; m19r_git "$omega" rev-parse HEAD && v=$(m19r_jstr "$M19R_GIT")
493 body+=",\"omega_head\":$v"
494 v=null; [ -d "${M19R_PHYSICS:-/nonexistent}" ] && m19r_git "$M19R_PHYSICS" rev-parse HEAD && v=$(m19r_jstr "$M19R_GIT")
495 body+=",\"physics_head\":$v"
496 body+=",\"candidate_trees_clean\":{\"omega\":$(num_tree_clean "$omega"),\"physics\":$([ -d "${M19R_PHYSICS:-/nonexistent}" ] && num_tree_clean "$M19R_PHYSICS" || echo null)}"
497 v=null; [ -z "${NUM_RUN_DIR:-}" ] || [ ! -f "$NUM_RUN_DIR/test_omega_numeric" ] ||
498 v="\"$(m19r_sha_file "$NUM_RUN_DIR/test_omega_numeric")\""
499 body+=",\"candidate_binary_sha256\":$v"
500 v=null; [[ ${NUM_EXEC_RC:-} =~ ^[0-9]+$ ]] && v=$NUM_EXEC_RC
501 body+=",\"gate_binary_exit_status\":$v"
502 v=null; [ -z "${NUM_RUN_DIR:-}" ] || [ ! -f "$NUM_RUN_DIR/gate5.log" ] ||
503 v="\"$(m19r_sha_file "$NUM_RUN_DIR/gate5.log")\""
504 body+=",\"gate_log_sha256\":$v"
505 v=null; [ -z "${NUM_RUN_DIR:-}" ] || [ ! -f "$NUM_RUN_DIR/gate5.stderr" ] ||
506 v="\"$(m19r_sha_file "$NUM_RUN_DIR/gate5.stderr")\""
507 body+=",\"gate_stderr_sha256\":$v}"
508 num_write_receipt "$omega" "$body" 0
509}
510
511num_qualify() {
512 local omega=$M19R_OMEGA bin before after rc
513 m19r_must_candidate "$omega" "$NUM_OMEGA_CAND" || return 1
514 num_check_physics "$omega" "$M19R_PHYSICS" "$NUM_PHYSICS_CAND" || return 1
515 before=$(m19r_historical) || return 1
516 bin=$NUM_RUN_DIR/test_omega_numeric
517
518 exec 9> /tmp/aien-gb10.lock || m19r_fail "cannot open /tmp/aien-gb10.lock" || return 1
519 flock -x 9 || { exec 9>&-; m19r_fail "cannot take the GPU lock /tmp/aien-gb10.lock; no build and no device access"; return 1; }
520 rc=0
521 num_build "$omega" "$M19R_PHYSICS" "$bin" "$NUM_RUN_DIR/build.log" || rc=1
522 if [ "$rc" = 0 ]; then
523 NUM_BINARY_SHA=$(m19r_sha_file "$bin")
524 if m19r_git "$omega" rev-parse HEAD; then NUM_RUN_COMMIT=$M19R_GIT; else rc=1; m19r_fail "cannot read the omega HEAD commit"; fi
525 NUM_TS=$(date -u +%Y-%m-%dT%H:%M:%S.%6NZ)
526 if [ "$rc" = 0 ]; then
527 num_execute "$omega" "$bin" || rc=1
528 echo "gate binary exit status $NUM_EXEC_RC" >> "$NUM_RUN_DIR/build.log"
529 fi
530 fi
531 exec 9>&-
532 [ "$rc" = 0 ] || return 1
533
534 after=$(m19r_historical) || return 1
535 [ "$before" = "$after" ] || m19r_fail "evidence/ changed during the run" || return 1
536 num_receipt "$omega" "$NUM_RUN_DIR/gate5.log"
537}
538
539# num_on_failure OMEGA REASON -- a failed run: forget any digest, remove the
540# PASS preview so nothing left behind claims a pass, and with NUM_RECORD=1
541# write the FAIL receipt.
542num_on_failure() {
543 NUM_DIGEST= NUM_PERMANENT=
544 rm -f "$NUM_RUN_DIR/receipt-preview.json"
545 if [ "${NUM_RECORD:-0}" = 1 ]; then
546 if num_fail_receipt "$1" "$2"; then
547 echo "Permanent FAIL receipt: $NUM_PERMANENT" >&2
548 else
549 echo "Could not write the FAIL receipt: $M19R_ERR" >&2
550 fi
551 else
552 echo "No receipt written (no --record)." >&2
553 fi
554}
555
556num_usage() {
557 echo "usage: $M19R_PROG --omega-candidate SHA --physics-candidate SHA [--physics-dir DIR] [--evidence-dir DIR] [--record]"
558}
559
560num_main() {
561 local physics_dir= rc=0 run_json
562 NUM_OMEGA_CAND= NUM_PHYSICS_CAND= NUM_RECORD=0 NUM_PERMANENT= NUM_DIGEST= NUM_EVIDENCE_DIR=
563 NUM_EXEC_RC= NUM_LOG_SHA= NUM_BINARY_SHA= NUM_RUN_COMMIT= NUM_TS=
564 while [ $# -gt 0 ]; do
565 case $1 in
566 -h|--help) num_usage; exit 0;;
567 --omega-candidate|--physics-candidate|--physics-dir|--evidence-dir)
568 [ $# -ge 2 ] || { num_usage >&2; echo "$M19R_PROG: $1 needs a value" >&2; exit 2; }
569 case $1 in
570 --omega-candidate) NUM_OMEGA_CAND=$2;;
571 --physics-candidate) NUM_PHYSICS_CAND=$2;;
572 --physics-dir) physics_dir=$2;;
573 --evidence-dir) NUM_EVIDENCE_DIR=$2;;
574 esac
575 shift;;
576 --omega-candidate=*) NUM_OMEGA_CAND=${1#*=};;
577 --physics-candidate=*) NUM_PHYSICS_CAND=${1#*=};;
578 --physics-dir=*) physics_dir=${1#*=};;
579 --evidence-dir=*) NUM_EVIDENCE_DIR=${1#*=};;
580 --record) NUM_RECORD=1;;
581 *) num_usage >&2; echo "$M19R_PROG: unrecognized argument: $1" >&2; exit 2;;
582 esac
583 shift
584 done
585 if [ -z "$NUM_OMEGA_CAND" ] || [ -z "$NUM_PHYSICS_CAND" ]; then
586 num_usage >&2; echo "$M19R_PROG: --omega-candidate and --physics-candidate are required" >&2; exit 2
587 fi
588 M19R_PHYSICS=$(realpath -m "${physics_dir:-$(dirname "$M19R_OMEGA")/physics}")
589 if ! num_check_evidence_dir "$M19R_OMEGA"; then
590 num_usage >&2; echo "$M19R_PROG: $M19R_ERR" >&2; exit 2
591 fi
592 m19r_build_canon || { echo "Gate 5 failed: cannot build tools/json_canon.c" >&2; exit 1; }
593 trap 'rm -rf "$M19R_TMP"' EXIT
594 NUM_RUN_ID=$(date -u +%Y%m%dT%H%M%SZ)-$(od -An -N6 -tx1 /dev/urandom | tr -d ' \n')
595 NUM_RUN_DIR=$M19R_OMEGA/build/qual-runs/gate5-$NUM_RUN_ID
596 mkdir -p "$(dirname "$NUM_RUN_DIR")" && mkdir "$NUM_RUN_DIR" || exit 1
597 rm -f "$M19R_TMP/error"
598 if num_qualify; then
599 echo "Gate 5 PASS: receipt digest $NUM_DIGEST"
600 [ -z "$NUM_PERMANENT" ] || echo "Permanent PASS receipt: $NUM_PERMANENT"
601 else
602 rc=1
603 [ ! -s "$M19R_TMP/error" ] || M19R_ERR=$(cat "$M19R_TMP/error")
604 echo "Gate 5 FAILED: $M19R_ERR" >&2
605 num_on_failure "$M19R_OMEGA" "$M19R_ERR"
606 fi
607 run_json="{\"run_id\":$(m19r_jstr "$NUM_RUN_ID"),\"status\":\"$([ "$rc" = 0 ] && echo PASS || echo FAILED)\""
608 run_json+=",\"physics_dir\":$(m19r_jstr "$M19R_PHYSICS")"
609 run_json+=",\"evidence_dir\":$(m19r_jstr "$NUM_EVIDENCE_DIR")"
610 run_json+=",\"trees_clean_after\":{\"omega\":$(num_tree_clean "$M19R_OMEGA"),\"physics\":$([ -d "$M19R_PHYSICS" ] && num_tree_clean "$M19R_PHYSICS" || echo null)}"
611 [ "$rc" = 0 ] || run_json+=",\"error\":$(m19r_jstr "$M19R_ERR")"
612 [ -z "$NUM_DIGEST" ] || run_json+=",\"receipt_digest\":\"$NUM_DIGEST\""
613 [ -z "$NUM_PERMANENT" ] || run_json+=",\"permanent_receipt\":$(m19r_jstr "$NUM_PERMANENT")"
614 run_json+="}"
615 printf '%s' "$run_json" | "$JSON_CANON" --pretty > "$NUM_RUN_DIR/run.json" || rc=1
616 echo "Gate 5 run evidence: $NUM_RUN_DIR/run.json"
617 exit "$rc"
618}
619
620if [ "${BASH_SOURCE[0]}" = "$0" ]; then
621 set -u
622 num_main "$@"
623fi