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 / m20_receipt.sh

218 lines · 11769 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# tools/m20_receipt.sh -- reproducible, content-addressed M20 OMEGA_TENSOR receipt.
18#
19# tools/m20_receipt.sh --tensor-log FILE --mutations-log FILE
20# [--evidence-dir DIR] [--repo DIR] [--chip-evidence FILE]
21# [--binary FILE]... [--started-utc T] [--finished-utc T]
22#
23# Inputs: the host gate logs of `make test-tensor` and `make
24# test-tensor-mutations`, optionally the GB10 chip evidence JSON written by
25# tests/run_tensor_chip.sh (only its path and bytes are used, never its code),
26# optional binaries to hash. Row labels come from the qualification table in
27# the committed docs/tensor/M20_OMEGA_TENSOR.md of the omega tree (--repo,
28# default: the tree holding this script).
29#
30# Table labels: "PASS (CPU)" or "host PASS" -> host PASS; "PASS (GB10)" or
31# "GB10 PASS" -> GB10 PASS; NOT_RUN; MISSING_IMPLEMENTATION; "WIRED" and
32# "PASS (on main)" -> REFERENCE (recorded, never counted). Anything else is
33# refused.
34#
35# Rules (each line carries a MUT:<name> marker; tools/m20_receipt_mutations.sh
36# breaks each one and requires tests/test_m20_receipt.sh to fail):
37# refused (exit 2, no receipt): dirty tree before; evidence dir inside the
38# tree; unknown label; host PASS rows while the host logs do not show PASS;
39# a GB10 PASS row without --chip-evidence, or whose evidence cell does not
40# name the sha256 of that file (first 64-hex string in the cell); an existing receipt with the same digest.
41# NOT QUALIFIED (exit 1, receipt written): any NOT_RUN or
42# MISSING_IMPLEMENTATION row; "GB10 parity" row not GB10 PASS; tree dirty or
43# HEAD moved after the inputs were stored; the test-only mid-run hook set.
44# QUALIFIED (exit 0) otherwise.
45# Output: <evidence-dir>/<sha256>.json (mode 0444, created exclusively, never
46# overwritten) and blobs/<sha256>.{log,json,bin}. Body is jq -S (sorted keys);
47# no clock is read, timestamps appear only when passed in, so the same inputs
48# give byte-identical JSON.
49# Shell + coreutils + git + jq. No Python.
50set -u
51SELF_DIR=$(cd -P "$(dirname "${BASH_SOURCE[0]}")" && pwd)
52REPO=$SELF_DIR/..
53EVID=$HOME/workspace/evidence-out/M20-OMEGA-TENSOR
54TLOG=""; MLOG=""; CHIP=""; STARTED=""; FINISHED=""
55BINS=()
56TABLE_REL=docs/tensor/M20_OMEGA_TENSOR.md
57
58refuse() { echo "m20_receipt: REFUSED: $*" >&2; exit 2; }
59need() { [ -n "${2:-}" ] || refuse "$1 needs a value"; }
60while [ $# -gt 0 ]; do
61 case $1 in
62 --repo) need "$1" "${2:-}"; REPO=$2; shift 2 ;;
63 --evidence-dir) need "$1" "${2:-}"; EVID=$2; shift 2 ;;
64 --tensor-log) need "$1" "${2:-}"; TLOG=$2; shift 2 ;;
65 --mutations-log) need "$1" "${2:-}"; MLOG=$2; shift 2 ;;
66 --chip-evidence) need "$1" "${2:-}"; CHIP=$2; shift 2 ;;
67 --binary) need "$1" "${2:-}"; BINS+=("$2"); shift 2 ;;
68 --started-utc) need "$1" "${2:-}"; STARTED=$2; shift 2 ;;
69 --finished-utc) need "$1" "${2:-}"; FINISHED=$2; shift 2 ;;
70 -h|--help) sed -n '2,34p' "$0"; exit 0 ;;
71 *) refuse "unknown argument $1" ;;
72 esac
73done
74command -v jq > /dev/null || refuse "jq not found"
75[ -f "$TLOG" ] || refuse "--tensor-log file missing"
76[ -f "$MLOG" ] || refuse "--mutations-log file missing"
77[ -z "$CHIP" ] || [ -f "$CHIP" ] || refuse "--chip-evidence file $CHIP missing"
78for b in ${BINS[@]+"${BINS[@]}"}; do [ -f "$b" ] || refuse "--binary file $b missing"; done
79
80TOP=$(git -C "$REPO" rev-parse --show-toplevel 2> /dev/null) || refuse "$REPO is not a git tree"
81TOP=$(cd -P "$TOP" && pwd)
82COMMIT=$(git -C "$TOP" rev-parse HEAD) || refuse "cannot read HEAD"
83[ -z "$(git -C "$TOP" status --porcelain)" ] || refuse "omega tree dirty before receipt" # MUT:DIRTY_REFUSED
84EVID_ABS=$(realpath -m "$EVID") || refuse "cannot resolve evidence dir"
85case "$EVID_ABS/" in "$TOP/"*) refuse "evidence dir $EVID_ABS lies inside the omega tree" ;; esac # MUT:EVID_OUTSIDE
86TABLE=$TOP/$TABLE_REL
87[ -f "$TABLE" ] || refuse "qualification table $TABLE_REL missing"
88
89WORK=$(mktemp -d "${TMPDIR:-/tmp}/m20-receipt.XXXXXX") || refuse "cannot create work dir"
90trap 'rm -rf "$WORK"' EXIT
91ROWS=$WORK/rows.tsv; REASONS=$WORK/reasons.txt; BINTSV=$WORK/bins.tsv
92: > "$ROWS"; : > "$REASONS"; : > "$BINTSV"
93NQ=0
94add_nq() { NQ=1; printf '%s\n' "$1" >> "$REASONS"; }
95trim() { printf '%s' "$1" | sed 's/^[[:space:]]*//; s/[[:space:]]*$//'; }
96sha() { sha256sum "$1" | cut -d' ' -f1; }
97
98# Host logs PASS: both test binaries (plain + ASan/UBSan) report N pass, 0 fail,
99# the no-hooks check passed, and the mutation sweep passed with nothing missed.
100host_logs_pass() {
101 local n
102 n=$(grep -cE '^M20 OMEGA_TENSOR CPU tests: [1-9][0-9]* pass, 0 fail$' "$TLOG")
103 [ "$n" -ge 2 ] || return 1
104 ! grep -qE '^M20 OMEGA_TENSOR CPU tests: [0-9]+ pass, [1-9][0-9]* fail' "$TLOG" || return 1
105 grep -q '^test-tensor-no-hooks: PASS' "$TLOG" || return 1
106 grep -q '^tensor mutation sweep: PASS' "$MLOG" || return 1
107 ! grep -qE 'sweep: FAIL|NOT CAUGHT|NOT APPLIED|does not build' "$MLOG"
108}
109
110TLOG_SHA=$(sha "$TLOG"); MLOG_SHA=$(sha "$MLOG"); TABLE_SHA=$(sha "$TABLE")
111CHIP_SHA=""; [ -z "$CHIP" ] || CHIP_SHA=$(sha "$CHIP")
112
113# Table rows: the pipe table after "## Qualification checklist", up to the
114# next heading or bold verdict line.
115awk '/^## Qualification checklist/ {on = 1; next}
116 on && (/^## / || /^\*\*/) {exit}
117 on && /^\|/ {if ($0 ~ /^\|[-: |]*\|[[:space:]]*$/) next; print}' "$TABLE" > "$WORK/table.txt"
118HAS_HOST=0; HAS_GB10=0; GB10_PARITY=0
119while IFS= read -r line; do
120 line=${line#|}; line=${line%|}
121 item=$(trim "${line%%|*}"); rest=${line#*|}
122 status=$(trim "${rest%%|*}"); evcell=""
123 [ "${rest#*|}" = "$rest" ] || evcell=${rest#*|}
124 [ "$item" = Item ] && continue
125 label=""; hw=""; ev=""
126 case $status in
127 "PASS (CPU)"|"host PASS") label="host PASS"; hw=host ;;
128 "PASS (GB10)"|"GB10 PASS") label="GB10 PASS"; hw=GB10 ;;
129 NOT_RUN|MISSING_IMPLEMENTATION) label=$status; hw=none ;;
130 WIRED|"PASS (on main)") label=REFERENCE; hw=none ;;
131 *) refuse "row '$item': unknown label '$status'" ;; # MUT:UNKNOWN_LABEL
132 esac
133 case $label in
134 NOT_RUN|MISSING_IMPLEMENTATION) add_nq "row '$item' is $label" ;; # MUT:NOTRUN_FORCES_NQ
135 "host PASS") HAS_HOST=1; ev="$TLOG_SHA,$MLOG_SHA" ;;
136 "GB10 PASS")
137 HAS_GB10=1
138 ehash=$(printf '%s' "$evcell" | grep -oE '[0-9a-f]{64}' | head -1)
139 [ -n "$CHIP" ] || refuse "row '$item' is GB10 PASS but no --chip-evidence file was given" # MUT:GB10_NEEDS_EVIDENCE
140 [ "$ehash" = "$CHIP_SHA" ] || refuse "row '$item': GB10 PASS evidence hash does not match the chip evidence file" # MUT:GB10_HASH_MATCH
141 [ "$item" != "GB10 parity" ] || GB10_PARITY=1
142 ev=$CHIP_SHA ;;
143 esac
144 printf '%s\t%s\t%s\t%s\n' "$label" "$hw" "$item" "$ev" >> "$ROWS"
145done < "$WORK/table.txt"
146[ -s "$ROWS" ] || refuse "no rows found in the qualification table"
147if [ "$HAS_HOST" = 1 ]; then host_logs_pass || refuse "host PASS rows but the host logs do not show PASS"; fi # MUT:HOST_LOG_PASS
148# M20 policy: only a GB10 parity row that is GB10 PASS with matching chip
149# evidence (checked above) can qualify; host-only evidence never does.
150[ "$GB10_PARITY" = 1 ] || add_nq "GB10 parity row is not GB10 PASS with matching chip evidence (host-only evidence never qualifies)" # MUT:GB10_PARITY_REQUIRED
151HW=host; [ "$HAS_GB10" = 0 ] || HW=GB10
152
153# store_blob SRC EXT -> prints the digest (pattern of tests/run_reduce_chip.sh).
154mkdir -p "$EVID_ABS/blobs" || refuse "cannot create $EVID_ABS/blobs"
155store_blob() {
156 local src=$1 ext=$2 d dst tmp have
157 d=$(sha "$src"); [ -n "$d" ] || refuse "cannot hash $src"
158 dst=$EVID_ABS/blobs/$d.$ext
159 if [ -e "$dst" ]; then
160 have=$(sha "$dst"); [ "$have" = "$d" ] || refuse "existing blob $dst does not match its digest"
161 else
162 tmp=$(mktemp "$EVID_ABS/blobs/.tmp.XXXXXX") || refuse "cannot create temp blob"
163 cp "$src" "$tmp" || { rm -f "$tmp"; refuse "cannot store $ext blob"; }
164 have=$(sha "$tmp"); [ "$have" = "$d" ] || { rm -f "$tmp"; refuse "stored $ext blob does not match its digest"; }
165 chmod 0444 "$tmp" || { rm -f "$tmp"; refuse "cannot seal $ext blob"; }
166 mv -n "$tmp" "$dst"; rm -f "$tmp"
167 have=$(sha "$dst"); [ "$have" = "$d" ] || refuse "blob $dst does not match its digest after publish"
168 fi
169 echo "$d"
170}
171store_blob "$TLOG" log > /dev/null || exit 2
172store_blob "$MLOG" log > /dev/null || exit 2
173[ -z "$CHIP" ] || store_blob "$CHIP" json > /dev/null || exit 2
174for b in ${BINS[@]+"${BINS[@]}"}; do
175 d=$(store_blob "$b" bin) || exit 2
176 printf '%s\t%s\n' "$(basename "$b")" "$d" >> "$BINTSV"
177done
178LC_ALL=C sort -o "$BINTSV" "$BINTSV"
179
180# Test-only: lets tests/test_m20_receipt.sh change the tree mid-run. Setting
181# it always forces NOT QUALIFIED.
182if [ -n "${M20_RECEIPT_TEST_MIDRUN:-}" ]; then
183 add_nq "test-only mid-run hook was set"
184 sh -c "$M20_RECEIPT_TEST_MIDRUN"
185fi
186CLEAN_AFTER=false; [ -n "$(git -C "$TOP" status --porcelain)" ] || CLEAN_AFTER=true
187UNCHANGED=false; [ "$(git -C "$TOP" rev-parse HEAD)" != "$COMMIT" ] || UNCHANGED=true
188[ "$CLEAN_AFTER" = true ] || add_nq "omega tree dirty after the inputs were stored" # MUT:CLEAN_AFTER
189[ "$UNCHANGED" = true ] || add_nq "HEAD moved during the receipt run" # MUT:COMMIT_UNCHANGED
190VERDICT=QUALIFIED; [ "$NQ" = 0 ] || VERDICT="NOT QUALIFIED"
191
192TMP=$WORK/receipt.json
193jq -S -n --arg commit "$COMMIT" --argjson clean_after "$CLEAN_AFTER" --argjson unchanged "$UNCHANGED" \
194 --arg table "$TABLE_REL" --arg table_sha "$TABLE_SHA" --rawfile rows "$ROWS" --arg hw "$HW" \
195 --arg tlog "$TLOG_SHA" --arg mlog "$MLOG_SHA" --arg chip "$CHIP_SHA" --rawfile bins "$BINTSV" \
196 --rawfile reasons "$REASONS" --arg verdict "$VERDICT" --arg started "$STARTED" --arg finished "$FINISHED" '
197 def nlines($s): $s | split("\n") | map(select(length > 0));
198 {gate: "M20_OMEGA_TENSOR", writer: "tools/m20_receipt.sh v1",
199 omega_commit: $commit, omega_tree_clean_before: true, omega_tree_clean_after: $clean_after,
200 omega_commit_unchanged_after: $unchanged,
201 qualification_table: {path: $table, sha256: $table_sha},
202 rows: [nlines($rows)[] | split("\t") |
203 {row: .[2], label: .[0], hardware: .[1],
204 evidence_sha256: (if .[3] == "" then [] else (.[3] | split(",")) end)}],
205 hardware: $hw,
206 logs: {test_tensor_sha256: $tlog, test_tensor_mutations_sha256: $mlog},
207 chip_evidence_sha256: (if $chip == "" then null else $chip end),
208 binaries: [nlines($bins)[] | split("\t") | {name: .[0], sha256: .[1]}],
209 verdict: $verdict, verdict_reasons: nlines($reasons)}
210 + (if $started == "" then {} else {started_utc: $started} end)
211 + (if $finished == "" then {} else {finished_utc: $finished} end)' > "$TMP" \
212 || refuse "receipt json (jq) failed, no receipt written"
213[ -s "$TMP" ] || refuse "empty receipt, not written"
214DIG=$(sha "$TMP")
215(set -o noclobber; cat "$TMP" > "$EVID_ABS/$DIG.json") 2> /dev/null || refuse "receipt $DIG.json already exists, never overwritten" # MUT:NO_OVERWRITE
216chmod 0444 "$EVID_ABS/$DIG.json"
217echo "receipt: $EVID_ABS/$DIG.json verdict=$VERDICT hardware=$HW"
218[ "$VERDICT" = QUALIFIED ]