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

276 lines · 22053 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# chip_run.sh MANIFEST [--physics-dir DIR] [--evidence-dir DIR] [-- gate args]
18# Run one chip binary under evidence. The one place that knows how (ground truth:
19# tools/run_numeric_transc_gate.sh, tools/run_unwritten_trap.sh, tests/run_reduce_chip.sh).
20# The gcc line is the transc/unwritten-trap one; reduce differs (see NOT COVERED).
21# MANIFEST is shell syntax setting: GATE TEST_SOURCE SOURCES EXTRA_BUILD_SOURCES (relative
22# to the omega root, may use $PHYSICS and $HERE) RUN_ARGS VERDICT_RE PASS_LINE OWNER
23# EVIDENCE_DIR RAISE_QUIET TAKE_GPU_LOCK REFUSE_DIRTY_OMEGA (default 1) REFUSE_EXIT
24# (default 2; must be an integer 1..125, else exit 1 "CHIP_RUN: BAD_MANIFEST") REFUSE_VERDICT_LINE
25# (default "CHIP_RUN: NOT_RUN") REQUIRE_ALL_PASS (default 0; 0 or 1; when 1 EVERY line matching
26# VERDICT_RE must also match PASS_LINE, so set VERDICT_RE to verdict lines only), CHILD_EXIT_PASSTHROUGH
27# (default 0; 0 or 1; when 1 and the binary exited nonzero, this module exits with the binary's
28# exit code, receipt and verdict still written; exit 0 with a non-PASS verdict still gives 1), hooks
29# GPU_LOCK_MODE (default flock = flock -n after the host tier; fuser = refuse early, before the host tier, if ANY process has
30# the lock file open (old transc test, text "<lock> is held"), then take it with a blocking flock -x),
31# FINAL_LINE_STYLE (default chip_run: last line "CHIP_RUN: <verdict> [reason]", fatal errors "CHIP_RUN: FAIL why"; verdict: last line
32# "VERDICT <verdict>", fatal errors print "why" then "VERDICT FAIL"), ECHO_RE (default empty = echo the whole chip log; else echo only lines matching it; the chip log blob and
33# the verdict are unaffected), HOST_TIER_CMD (run first; nonzero refuses) and RECEIPT_EXTRA_JQ (jq filter on the receipt,
34# $log = chip log, $hostlog = the whole HOST_TIER_CMD output, empty if none; it may replace a FAIL
35# verdict with another upper-case word such as NOT_RUN, never create or remove a PASS). The manifest cannot change the test seam variables below, QUIET_FLAG,
36# GPU_LOCK or EST_LOAD_CMD (checked and restored after it is sourced). That is an accident guard,
37# not a boundary: the manifest is sourced into this shell and could still set any other variable.
38# Gate args after "--" REPLACE the manifest RUN_ARGS (not appended); RUN_ARGS is split on whitespace.
39# receipt host_tier = last line of the HOST_TIER_CMD log (empty if none); only its last 3 lines are
40# printed. HOST_TIER_CMD runs in the omega root with CHIPRUN_RUN_DIR set, before the quiet flag and lock.
41# Physics dir, first match wins: --physics-dir, $PHYSICS_DIR, $PHYSICS, ~/workspace/physics.
42# Exit: 0 PASS; 1 FAIL (binary ran, verdict not PASS or tree changed); REFUSE_EXIT (default 2,
43# as run_unwritten_trap.sh) after printing "REFUSED: why" and REFUSE_VERDICT_LINE. A gate that
44# keeps transc semantics sets REFUSE_EXIT=1 REFUSE_VERDICT_LINE="VERDICT NOT_RUN".
45# Those two apply only to refusals after the manifest is sourced; usage, argument, seam-before and
46# missing-manifest refusals always exit 2 with "CHIP_RUN: NOT_RUN". Exit 1 is also BAD_MANIFEST and
47# the post-run "fatal" paths (log blob or receipt not written): "CHIP_RUN: FAIL ...", no receipt promised.
48# REFUSES (the chip binary is not run; HOST_TIER_CMD, gcc and nm may already have run): bad usage,
49# argument or manifest; missing manifest variables or GATE characters; no evidence dir; test override env without CHIPRUN_SELFTEST=1 (checked before and
50# after the manifest is sourced; a manifest that changes one is refused); no physics checkout;
51# unreadable omega or physics HEAD or physics.lock; physics not at the physics.lock pin;
52# physics dirty; omega dirty (if REFUSE_DIRTY_OMEGA=1); evidence dir inside omega or physics;
53# quiet flag already up or lost in a create race (if RAISE_QUIET=1); an est_load process; the
54# GPU lock unopenable or held (if TAKE_GPU_LOCK=1); no temp dir; HOST_TIER_CMD failing; a
55# missing source; gcc failing; nm unreadable; libm math or CUDA symbols in the binary. The libm check
56# is a fixed list: sqrt exp exp2 log log2 pow fma sin cos tanh erf round fabs and their f variants;
57# floor, ceil, fmod, tan, sinh, atan, log10 and the rest are not caught.
58# FAILS (ran to the end, receipt written): nonzero exit; no line matching VERDICT_RE; last such
59# line not matching PASS_LINE (any such line, if REQUIRE_ALL_PASS=1); omega (if
60# REFUSE_DIRTY_OMEGA=1) or physics dirty after the run; either HEAD moved. Never killed, never
61# timed out. Quiet flag dropped only if still ours. The receipt "reason" holds only the first failure.
62# RECEIPT <evidence>/<sha256 of its content>.json, mode 0444, noclobber, plus blobs/<sha>.log.
63# Fields: gate owner omega_commit omega_tree_clean_before omega_tree_clean_after
64# omega_commit_unchanged_after physics_commit physics_lock_pin physics_tree_clean_before
65# physics_tree_clean_after physics_commit_unchanged_after binary_sha256 chip_log_sha256
66# run_args host_tier verdict_lines chip_exit_status started_utc finished_utc verdict reason
67# (plus whatever RECEIPT_EXTRA_JQ adds).
68# NOT COVERED by this module (a gate that needs one adds it, or does not use this module yet):
69# - transc's kernel-digest field and unknown-op check: RECEIPT_EXTRA_JQ over $hostlog, and the wrapper; its nvdisasm check: HOST_TIER_CMD.
70# - transc locks with fuser; set GPU_LOCK_MODE=fuser for that (default flock -n is the other test).
71# - reduce WAITS for the lock and the quiet flag; this module REFUSES at once.
72# - binary blob storage: reduce stores blobs/<sha>.bin and refuses a PASS with no digests;
73# this module stores only the log blob, and records binary_sha256 without keeping the binary.
74# - REQUIRE_ALL_PASS checks lines that appear, not that a particular line appears: reduce's
75# SUM, MAX, MIN, MEAN, RED_GB10_PARITY and E1 lines are not each demanded.
76# - receipt reuse (an existing receipt with the same name is compared, not rewritten) has no case.
77# - the build line: reduce (tests/run_reduce_chip.sh) adds -I"$PHYS/forge" (no variable here for it)
78# and has neither -fno-fast-math nor -pthread; this module uses the transc/unwritten-trap flags.
79# Test seam (only with CHIPRUN_SELFTEST=1): CHIPRUN_PREBUILT_BIN CHIPRUN_QUIET_FLAG
80# CHIPRUN_GPU_LOCK CHIPRUN_EST_LOAD_CMD. Mutation check: each refusal line ends in
81# "# REFUSAL:<id>"; tests/test_chip_run.sh --mutants deletes each one and needs case <id> to FAIL.
82# The restore of QUIET_FLAG, GPU_LOCK and EST_LOAD_CMD after the manifest is tagged the same way
83# (case restore_vars). --mutants also reverts the jq "x" prefix for the verdict lines and for the
84# run args and needs case dash_verdict or happy to FAIL. Not tagged: the "fatal" exits after the run.
85# Shell + GNU coreutils (date -d, realpath -m) + git + jq + gcc + nm + flock. No Python.
86set -u
87HERE=$(cd -P "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)
88NVDIR=third_party/nvidia-open-580.173.02 # the one definition of the NVIDIA header tree
89QUIET_FLAG=${CHIPRUN_QUIET_FLAG:-$HOME/workspace/.spark-quiet}
90GPU_LOCK=${CHIPRUN_GPU_LOCK:-/tmp/aien-gb10.lock}
91EST_LOAD_CMD=${CHIPRUN_EST_LOAD_CMD:-pgrep est_load}
92FLAG_MINE=0; FLAG_TEXT=""; FAIL_REASON=""
93REFUSE_EXIT=2; REFUSE_VERDICT_LINE="CHIP_RUN: NOT_RUN"
94refuse() { echo "REFUSED: $*"; echo "$REFUSE_VERDICT_LINE"; exit "$REFUSE_EXIT"; }
95bad_manifest() { echo "CHIP_RUN: BAD_MANIFEST $*"; exit 1; } # REFUSAL:bad_manifest_def
96FINAL_LINE_STYLE=chip_run # manifest may set "verdict": the last line is "VERDICT <word>" (old transc style)
97fatal() { if [ "$FINAL_LINE_STYLE" = verdict ]; then echo "$*"; echo "VERDICT FAIL"; else echo "CHIP_RUN: FAIL $*"; fi; exit 1; } # REFUSAL:fatal_verdict_style
98fail() { FAIL_REASON=${FAIL_REASON:-$*}; }
99cleanup() {
100 [ "$FLAG_MINE" = 1 ] || return 0
101 [ "$(cat "$QUIET_FLAG" 2>/dev/null)" = "$FLAG_TEXT" ] || return 0 # REFUSAL:flag_not_ours
102 rm -f "$QUIET_FLAG"; return 0
103}
104trap cleanup EXIT
105sha() { sha256sum "$1" | cut -d' ' -f1; }
106clean() { [ -z "$(git -C "$1" status --porcelain 2>/dev/null)" ] && echo true || echo false; }
107head_of() { git -C "$1" rev-parse HEAD 2>/dev/null; }
108
109# Test seam: snapshot before the manifest is sourced, check before and after, restore after.
110SEAM_VARS=(CHIPRUN_SELFTEST CHIPRUN_PREBUILT_BIN CHIPRUN_QUIET_FLAG CHIPRUN_GPU_LOCK CHIPRUN_EST_LOAD_CMD)
111declare -A SEAM0; for v in "${SEAM_VARS[@]}"; do SEAM0[$v]=${!v:-}; done
112SELFTEST0=${CHIPRUN_SELFTEST:-}; QUIET0=$QUIET_FLAG; GPU0=$GPU_LOCK; EST0=$EST_LOAD_CMD
113seam_check() {
114 [ -z "${CHIPRUN_PREBUILT_BIN:-}" ] || [ "$SELFTEST0" = 1 ] || refuse "CHIPRUN_PREBUILT_BIN is a self-test override; set CHIPRUN_SELFTEST=1" # REFUSAL:seam_prebuilt
115 [ -z "${CHIPRUN_QUIET_FLAG:-}" ] || [ "$SELFTEST0" = 1 ] || refuse "CHIPRUN_QUIET_FLAG is a self-test override; set CHIPRUN_SELFTEST=1" # REFUSAL:seam_quiet
116 [ -z "${CHIPRUN_GPU_LOCK:-}" ] || [ "$SELFTEST0" = 1 ] || refuse "CHIPRUN_GPU_LOCK is a self-test override; set CHIPRUN_SELFTEST=1" # REFUSAL:seam_gpu
117 [ -z "${CHIPRUN_EST_LOAD_CMD:-}" ] || [ "$SELFTEST0" = 1 ] || refuse "CHIPRUN_EST_LOAD_CMD is a self-test override; set CHIPRUN_SELFTEST=1" # REFUSAL:seam_est
118}
119
120[ $# -ge 1 ] || refuse "usage: chip_run.sh MANIFEST [--physics-dir DIR] [--evidence-dir DIR] [-- gate args]" # REFUSAL:usage
121MANIFEST=$1; shift
122PHYS_FLAG=""; EVID_FLAG=""; BAD_ARG=""
123while [ $# -gt 0 ]; do
124 case $1 in --physics-dir|--evidence-dir) [ $# -ge 2 ] || refuse "$1 needs a value" ;; esac # REFUSAL:arg_value
125 case $1 in
126 --physics-dir) PHYS_FLAG=$2; shift 2 ;;
127 --evidence-dir) EVID_FLAG=$2; shift 2 ;;
128 --) shift; break ;;
129 *) BAD_ARG=$1; break ;;
130 esac
131done
132[ -z "$BAD_ARG" ] || refuse "unknown argument $BAD_ARG" # REFUSAL:bad_arg
133GATE_ARGS=("$@")
134seam_check # REFUSAL:seam_before
135
136# Physics dir resolution lives here and nowhere else.
137PHYSICS=${PHYS_FLAG:-${PHYSICS_DIR:-${PHYSICS:-$HOME/workspace/physics}}}
138PHYSICS=$(realpath -m "$PHYSICS")
139
140GATE=""; TEST_SOURCE=""; SOURCES=""; EXTRA_BUILD_SOURCES=""; RUN_ARGS=""; VERDICT_RE=""; PASS_LINE=""
141OWNER=""; EVIDENCE_DIR=""; RAISE_QUIET=1; TAKE_GPU_LOCK=1; REFUSE_DIRTY_OMEGA=1; HOST_TIER_CMD=""; RECEIPT_EXTRA_JQ=""; REQUIRE_ALL_PASS=0; CHILD_EXIT_PASSTHROUGH=0; ECHO_RE=""; GPU_LOCK_MODE=flock
142[ -f "$MANIFEST" ] || refuse "no manifest at $MANIFEST" # REFUSAL:no_manifest
143. "$MANIFEST"; MRC=$?
144REX_BAD=""
145case $REFUSE_EXIT in ''|*[!0-9]*) REX_BAD=1 ;; *) { [ "${#REFUSE_EXIT}" -le 3 ] && [ "$((10#$REFUSE_EXIT))" -ge 1 ] && [ "$((10#$REFUSE_EXIT))" -le 125 ]; } || REX_BAD=1 ;; esac
146[ -z "$REX_BAD" ] || bad_manifest "REFUSE_EXIT=$REFUSE_EXIT is not an integer from 1 to 125" # REFUSAL:refuse_exit
147case $REQUIRE_ALL_PASS in 0|1) ;; *) bad_manifest "REQUIRE_ALL_PASS=$REQUIRE_ALL_PASS is not 0 or 1" ;; esac # REFUSAL:require_all_value
148case $CHILD_EXIT_PASSTHROUGH in 0|1) ;; *) bad_manifest "CHILD_EXIT_PASSTHROUGH=$CHILD_EXIT_PASSTHROUGH is not 0 or 1" ;; esac # REFUSAL:child_exit_value
149case $FINAL_LINE_STYLE in chip_run|verdict) ;; *) bad_manifest "FINAL_LINE_STYLE=$FINAL_LINE_STYLE is not chip_run or verdict" ;; esac # REFUSAL:final_style_value
150case $GPU_LOCK_MODE in flock|fuser) ;; *) bad_manifest "GPU_LOCK_MODE=$GPU_LOCK_MODE is not flock or fuser" ;; esac # REFUSAL:gpu_mode_value
151seam_check # REFUSAL:seam_after
152for v in "${SEAM_VARS[@]}"; do [ "${!v:-}" = "${SEAM0[$v]}" ] || refuse "manifest changed $v, a test seam variable"; done # REFUSAL:seam_changed
153QUIET_FLAG=$QUIET0; GPU_LOCK=$GPU0; EST_LOAD_CMD=$EST0 # REFUSAL:restore_vars
154[ "$MRC" = 0 ] || refuse "manifest $MANIFEST failed to load" # REFUSAL:manifest_load
155for v in GATE TEST_SOURCE VERDICT_RE PASS_LINE OWNER; do [ -n "${!v}" ] || refuse "manifest does not set $v"; done # REFUSAL:manifest_vars
156case $GATE in *[!A-Za-z0-9_.-]*) refuse "GATE $GATE has characters outside A-Za-z0-9_.-" ;; esac # REFUSAL:gate_chars
157EVID=${EVID_FLAG:-$EVIDENCE_DIR}
158[ -n "$EVID" ] || refuse "no evidence dir (manifest EVIDENCE_DIR or --evidence-dir)" # REFUSAL:no_evidence
159EVID=$(realpath -m "$EVID")
160if [ "${#GATE_ARGS[@]}" -eq 0 ]; then read -ra GATE_ARGS <<< "$RUN_ARGS"; fi
161
162[ -d "$PHYSICS/nvrm" ] || refuse "no physics checkout at $PHYSICS" # REFUSAL:no_physics
163case "$EVID/" in "$HERE"/*) refuse "evidence dir $EVID is inside a candidate tree" ;; esac # REFUSAL:evidence_inside
164case "$EVID/" in "$PHYSICS"/*) refuse "evidence dir $EVID is inside a candidate tree" ;; esac # REFUSAL:evidence_inside_physics
165OMEGA_COMMIT=$(head_of "$HERE") || refuse "cannot read omega HEAD" # REFUSAL:omega_head
166[ "$REFUSE_DIRTY_OMEGA" != 1 ] || [ "$(clean "$HERE")" = true ] || refuse "omega tree $HERE is dirty" # REFUSAL:omega_dirty
167PIN=$(tr -d '[:space:]' < "$HERE/physics.lock") || refuse "cannot read physics.lock" # REFUSAL:physics_lock
168PHYS_COMMIT=$(head_of "$PHYSICS") || refuse "cannot read physics HEAD at $PHYSICS" # REFUSAL:physics_head
169[ "$PHYS_COMMIT" = "$PIN" ] || refuse "physics checkout is at $PHYS_COMMIT, physics.lock pins $PIN" # REFUSAL:physics_pin
170[ "$(clean "$PHYSICS")" = true ] || refuse "physics checkout $PHYSICS is dirty" # REFUSAL:physics_dirty
171OMEGA_CLEAN_BEFORE=$(clean "$HERE"); PHYS_CLEAN_BEFORE=true
172[ "$RAISE_QUIET" != 1 ] || [ ! -e "$QUIET_FLAG" ] || refuse "quiet flag is up: $(cat "$QUIET_FLAG")" # REFUSAL:quiet_flag
173[ -z "$(bash -c "$EST_LOAD_CMD" 2>/dev/null)" ] || refuse "an est_load process is running" # REFUSAL:est_load
174[ "$TAKE_GPU_LOCK" != 1 ] || [ "$GPU_LOCK_MODE" != fuser ] || ! fuser "$GPU_LOCK" > /dev/null 2>&1 || refuse "$GPU_LOCK is held" # REFUSAL:gpu_fuser
175
176RUN=$(mktemp -d "${TMPDIR:-/tmp}/chip-run.XXXXXX") || refuse "cannot create a run directory" # REFUSAL:mktemp
177if [ -n "$HOST_TIER_CMD" ]; then
178 (cd "$HERE" && CHIPRUN_RUN_DIR="$RUN" bash -c "$HOST_TIER_CMD") > "$RUN/host.log" 2>&1; HRC=$?
179 tail -n 3 "$RUN/host.log"
180 [ "$HRC" = 0 ] || refuse "host tier failed (exit $HRC); no chip run" # REFUSAL:host_tier
181fi
182[ -e "$RUN/host.log" ] || : > "$RUN/host.log" # seam: $hostlog is always readable by RECEIPT_EXTRA_JQ
183HOST_TIER=$(tail -n 1 "$RUN/host.log" 2>/dev/null)
184
185START=$(date -u +%Y-%m-%dT%H:%M:%SZ)
186if [ "$RAISE_QUIET" = 1 ]; then
187 FLAG_TEXT="$OWNER start=$START expected_end=$(date -u -d '+2 hours' +%Y-%m-%dT%H:%M:%SZ) pid=$$"
188 set -o noclobber
189 echo "$FLAG_TEXT" > "$QUIET_FLAG" 2>/dev/null || { set +o noclobber; refuse "could not create the quiet flag"; } # REFUSAL:flag_create
190 set +o noclobber
191 FLAG_MINE=1
192fi
193if [ "$TAKE_GPU_LOCK" = 1 ]; then
194 { : >> "$GPU_LOCK"; } 2>/dev/null || refuse "cannot open $GPU_LOCK" # REFUSAL:gpu_open
195 exec 9>> "$GPU_LOCK"
196 [ "$GPU_LOCK_MODE" != fuser ] || flock -x 9 || refuse "cannot take $GPU_LOCK" # fuser mode: the early fuser check refused any holder, now wait for the lock like the old transc script
197 [ "$GPU_LOCK_MODE" = fuser ] || flock -n 9 || refuse "GPU lock $GPU_LOCK is held" # REFUSAL:gpu_lock
198fi
199
200if [ -n "${CHIPRUN_PREBUILT_BIN:-}" ]; then
201 BIN=$CHIPRUN_PREBUILT_BIN
202 [ -x "$BIN" ] || refuse "CHIPRUN_PREBUILT_BIN $BIN is not executable" # REFUSAL:prebuilt_not_exec
203else
204 echo "== chip build"
205 BIN=$RUN/$GATE.bin
206 read -ra SRCS <<< "$TEST_SOURCE $SOURCES $EXTRA_BUILD_SOURCES"
207 for s in "${SRCS[@]}"; do case $s in /*) f=$s ;; *) f=$HERE/$s ;; esac; [ -f "$f" ] || refuse "missing source $s"; done # REFUSAL:missing_source
208 NV=$PHYSICS/$NVDIR
209 (cd "$HERE" && gcc -std=gnu11 -O2 -Wall -Wextra -Werror -ffp-contract=off -fno-fast-math -pthread \
210 -Isrc -I"$PHYSICS/nvrm" -I"$PHYSICS/m16" \
211 -I"$NV/src/common/sdk/nvidia/inc" -I"$NV/kernel-open/common/inc" \
212 -I"$NV/kernel-open/nvidia-uvm" -I"$NV/src/nvidia/arch/nvalloc/unix/include" \
213 -o "$BIN" "${SRCS[@]}") > "$RUN/chip-build.log" 2>&1; BRC=$?
214 [ "$BRC" = 0 ] || { cat "$RUN/chip-build.log"; refuse "chip build failed"; } # REFUSAL:build_failed
215fi
216SYMS=$(nm -u "$BIN" 2>&1) || refuse "nm cannot read $BIN" # REFUSAL:nm_unreadable
217if printf '%s\n' "$SYMS" | grep -Eq '\b(sqrtf?|expf?|exp2f?|logf?|log2f?|powf?|fmaf?|sinf?|cosf?|tanhf?|erff?|roundf?|fabsf?)\b'; then refuse "libm math symbols in $BIN"; fi # REFUSAL:libm
218if printf '%s\n' "$SYMS" | grep -Eiq 'cuda|cuInit|cuLaunch|nvrtc|cublas'; then refuse "CUDA symbols in $BIN"; fi # REFUSAL:cuda
219BIN_SHA=$(sha "$BIN")
220
221echo "== chip run (not killed, not timed out): ${GATE_ARGS[*]}"
222(cd "$HERE" && "$BIN" "${GATE_ARGS[@]}") > "$RUN/chip.log" 2>&1; CHIP_RC=$?
223END=$(date -u +%Y-%m-%dT%H:%M:%SZ)
224exec 9>&-
225mapfile -t VLINES < <(grep -E -- "$VERDICT_RE" "$RUN/chip.log")
226if [ -n "$ECHO_RE" ]; then grep -E -- "$ECHO_RE" "$RUN/chip.log"; else cat "$RUN/chip.log"; fi # default: the full chip log goes to stdout (HIT and DEVERR lines included); seam: ECHO_RE limits the echo
227
228OMEGA_CLEAN_AFTER=$(clean "$HERE"); PHYS_CLEAN_AFTER=$(clean "$PHYSICS")
229OMEGA_SAME=$([ "$(head_of "$HERE")" = "$OMEGA_COMMIT" ] && echo true || echo false)
230PHYS_SAME=$([ "$(head_of "$PHYSICS")" = "$PHYS_COMMIT" ] && echo true || echo false)
231[ "$CHIP_RC" = 0 ] || fail "binary exit status $CHIP_RC" # REFUSAL:rc_nonzero
232[ "${#VLINES[@]}" -gt 0 ] || fail "no verdict line matching $VERDICT_RE" # REFUSAL:no_verdict
233[ "${#VLINES[@]}" -eq 0 ] || printf '%s\n' "${VLINES[$((${#VLINES[@]} - 1))]}" | grep -Eq -- "$PASS_LINE" || fail "last verdict line does not match PASS_LINE" # REFUSAL:fail_verdict
234if [ "$REQUIRE_ALL_PASS" = 1 ] && [ "${#VLINES[@]}" -gt 0 ] && printf '%s\n' "${VLINES[@]}" | grep -Evq -- "$PASS_LINE"; then fail "a verdict line does not match PASS_LINE (REQUIRE_ALL_PASS=1)"; fi # REFUSAL:fail_any_verdict
235[ "$REFUSE_DIRTY_OMEGA" != 1 ] || [ "$OMEGA_CLEAN_AFTER" = true ] || fail "omega tree dirty after run" # REFUSAL:omega_dirty_after
236[ "$PHYS_CLEAN_AFTER" = true ] || fail "physics tree dirty after run" # REFUSAL:physics_dirty_after
237[ "$OMEGA_SAME" = true ] && [ "$PHYS_SAME" = true ] || fail "a HEAD moved during the run" # REFUSAL:head_moved
238VERDICT=PASS; [ -z "$FAIL_REASON" ] || VERDICT=FAIL
239
240LOG_SHA=$(sha "$RUN/chip.log")
241mkdir -p "$EVID/blobs" || fatal "cannot create $EVID/blobs"
242if [ ! -e "$EVID/blobs/$LOG_SHA.log" ]; then
243 TMPB=$(mktemp "$EVID/blobs/.tmp.XXXXXX") && cp "$RUN/chip.log" "$TMPB" && chmod 0444 "$TMPB" && mv -n "$TMPB" "$EVID/blobs/$LOG_SHA.log" || fatal "could not store the log blob"
244 rm -f "$TMPB"
245fi
246[ "$(sha "$EVID/blobs/$LOG_SHA.log")" = "$LOG_SHA" ] || fatal "log blob does not match its digest"
247# The two arrays go to jq with an "x" prefix that jq strips: jq 1.7 reads an --args value that starts with "--" or with "-" and a letter as an option.
248BODY=$(jq -n --arg gate "$GATE" --arg owner "$OWNER" --arg omega "$OMEGA_COMMIT" --arg phys "$PHYS_COMMIT" --arg pin "$PIN" \
249 --argjson oclean "$OMEGA_CLEAN_BEFORE" --argjson pclean "$PHYS_CLEAN_BEFORE" \
250 --argjson oafter "$OMEGA_CLEAN_AFTER" --argjson pafter "$PHYS_CLEAN_AFTER" --argjson osame "$OMEGA_SAME" --argjson psame "$PHYS_SAME" \
251 --arg bin "$BIN_SHA" --arg log "$LOG_SHA" --arg host "$HOST_TIER" --argjson rc "$CHIP_RC" \
252 --arg start "$START" --arg end "$END" --arg verdict "$VERDICT" --arg reason "$FAIL_REASON" \
253 --argjson run_args "$(jq -n '$ARGS.positional | map(.[1:])' --args "${GATE_ARGS[@]/#/x}")" \
254 --argjson vlines "$(jq -n '$ARGS.positional | map(.[1:])' --args "${VLINES[@]/#/x}")" '{
255 gate: $gate, owner: $owner, omega_commit: $omega, omega_tree_clean_before: $oclean, omega_tree_clean_after: $oafter,
256 omega_commit_unchanged_after: $osame, physics_commit: $phys, physics_lock_pin: $pin, physics_tree_clean_before: $pclean,
257 physics_tree_clean_after: $pafter, physics_commit_unchanged_after: $psame, binary_sha256: $bin, chip_log_sha256: $log,
258 run_args: $run_args, host_tier: $host, verdict_lines: $vlines, chip_exit_status: $rc, started_utc: $start,
259 finished_utc: $end, verdict: $verdict, reason: $reason }') || fatal "receipt JSON could not be built"
260if [ -n "$RECEIPT_EXTRA_JQ" ]; then BODY=$(printf '%s' "$BODY" | jq --rawfile log "$RUN/chip.log" --rawfile hostlog "$RUN/host.log" "$RECEIPT_EXTRA_JQ") || fatal "RECEIPT_EXTRA_JQ failed"; fi
261printf '%s\n' "$BODY" > "$RUN/receipt.json"
262jq -e '.gate and .verdict and .binary_sha256' "$RUN/receipt.json" > /dev/null || fatal "receipt lost its required fields"
263RVERDICT=$(jq -r ".verdict | strings" "$RUN/receipt.json" 2>/dev/null)
264case $RVERDICT in ""|*[!A-Z_]*) fatal "receipt verdict is not an upper-case word" ;; esac # REFUSAL:verdict_word
265[ "$VERDICT" != PASS ] || [ "$RVERDICT" = PASS ] || fatal "RECEIPT_EXTRA_JQ changed a PASS verdict" # REFUSAL:verdict_pass_changed
266[ "$VERDICT" = PASS ] || [ "$RVERDICT" != PASS ] || fatal "RECEIPT_EXTRA_JQ forged a PASS verdict" # REFUSAL:verdict_forged
267VERDICT=$RVERDICT # seam: RECEIPT_EXTRA_JQ may replace a FAIL with another non-PASS word (for example NOT_RUN); PASS cannot be forged
268RSHA=$(sha "$RUN/receipt.json"); OUT=$EVID/$RSHA.json
269if [ ! -e "$OUT" ]; then
270 set -o noclobber; { cat "$RUN/receipt.json" > "$OUT" && chmod 0444 "$OUT"; } || { set +o noclobber; fatal "could not write receipt $OUT"; }; set +o noclobber
271fi
272cmp -s "$RUN/receipt.json" "$OUT" || fatal "receipt $OUT does not match what was built"
273echo "RECEIPT $OUT"
274if [ "$FINAL_LINE_STYLE" = verdict ]; then echo "VERDICT $VERDICT"; else echo "CHIP_RUN: $VERDICT${FAIL_REASON:+ $FAIL_REASON}"; fi # REFUSAL:final_style
275[ "$CHILD_EXIT_PASSTHROUGH" = 1 ] && [ "$CHIP_RC" != 0 ] && exit "$CHIP_RC" # REFUSAL:child_exit_passthrough
276[ "$VERDICT" = PASS ]