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

426 lines · 24441 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# r15_qualify.sh -- the R15 qualification run (spec/r15-performance-proof.md
18# §9, §10, §12, §13, §16 C1 item 7).
19#
20# tools/r15_qualify.sh [--detach] <host|silicon> [rounds=12] [l1_runs=5] [l2_runs=5]
21# (env: AIENOS_LOCK_REPO or AIENOS_R7_DIR, PHYSICS_DIR repositories holding
22# the pinned commits; CC; R15_OUT_BASE to write a dry run outside evidence/)
23#
24# Runs, in order: W-EPISODE trials (rounds x RES-4/RES-1/SEQ/RES-1-NODIGEST,
25# balanced Latin square), Level 1 (A,C,D,E,G,W0,W1 for RES-1 and SEQ
26# interleaved, B for RES-1), Level 2 (RES-1/SEQ interleaved). Every process
27# writes its own raw file; a failed process is recorded and NEVER rerun (§10).
28# The benchmark and reducer are rebuilt from verified Git archives in a private
29# directory. Only digest-bound copies under the run directory are executed.
30# Then SHA256SUMS and the reducer. Output: evidence/R15/raw/<run-id>/.
31#
32# --detach re-launches this script in its own session (setsid nohup), so an
33# agent or terminal going away cannot stop a long run; progress is written in
34# plain words to <out>/progress.log.
35#
36# The only machine setting changed is kernel.perf_event_paranoid: 4 -> 1 for
37# the run, restored to 4 on every exit path, and the run fails if it does not
38# read back 4. No systemd.
39set -u
40set -o pipefail
41
42if [ "${1:-}" = "--detach" ]; then
43 shift
44 setsid nohup "$0" "$@" </dev/null >/dev/null 2>&1 &
45 disown
46 echo "started in the background (pid $!)"
47 exit 0
48fi
49
50MODE=${1:?host or silicon}
51ROUNDS=${2:-12}
52L1RUNS=${3:-5}
53L2RUNS=${4:-5}
54case "$MODE" in host|silicon) ;; *) echo "mode must be host or silicon" >&2; exit 64;; esac
55
56HERE=$(cd "$(dirname "$0")/.." && pwd)
57cd "$HERE" || exit 1
58export GIT_NO_REPLACE_OBJECTS=1 GIT_GRAFT_FILE=/nonexistent/r15-qualify-grafts
59COMMIT=$(git rev-parse HEAD) || { echo "r15_qualify: cannot read candidate HEAD" >&2; exit 2; }
60INITIAL_STATUS=$(git status --porcelain --untracked-files=no) || { echo "r15_qualify: cannot read initial tree state" >&2; exit 2; }
61DIRTY=$([ -z "$INITIAL_STATUS" ] && echo false || echo true)
62RUN_ID=$(date -u +%Y%m%dT%H%M%SZ)-${COMMIT:0:12}-$MODE
63OUT_BASE=${R15_OUT_BASE:-$HERE/evidence/R15/raw}
64mkdir -p "$OUT_BASE" || exit 1
65OUT=$OUT_BASE/$RUN_ID
66mkdir -m 0755 "$OUT" || { echo "r15_qualify: run directory already exists or cannot be created: $OUT" >&2; exit 1; }
67LOG=$OUT/progress.log
68say() { echo "$(date '+%H:%M:%S') $*" >> "$LOG"; }
69jstr() { printf '"%s"' "$(printf '%s' "$1" | tr -d '"\\' | tr '\n' ' ')"; }
70sha_file() { sha256sum "$1" | cut -d' ' -f1; }
71full_commit() { [[ "$1" =~ ^[0-9a-f]{40}$ ]]; }
72plain_path() { [[ "$1" =~ ^[A-Za-z0-9/._+-]+$ ]]; }
73
74BUILD_LOG=$OUT/build.log
75: > "$BUILD_LOG"
76prepare_fail() {
77 say "PREPARATION FAILED: $*"
78 echo "r15_qualify: $*" >&2
79 exit 2
80}
81verify_commit() { # <repository> <commit> <label>; prints the root tree id
82 local repo=$1 commit=$2 label=$3 got tree line
83 full_commit "$commit" || prepare_fail "$label commit is not one full lower-case SHA-1: $commit"
84 got=$(git -C "$repo" -c core.commitGraph=false -c core.hooksPath=/dev/null -c core.fsmonitor= \
85 rev-parse --verify "$commit^{commit}" 2>/dev/null) || prepare_fail "$label commit $commit is unavailable in $repo"
86 [ "$got" = "$commit" ] || prepare_fail "$label resolved to $got, not $commit"
87 got=$(git -C "$repo" -c core.commitGraph=false -c core.hooksPath=/dev/null -c core.fsmonitor= \
88 cat-file commit "$commit" 2>/dev/null | git hash-object -t commit --stdin 2>/dev/null) || prepare_fail "cannot re-hash $label commit $commit"
89 [ "$got" = "$commit" ] || prepare_fail "$label commit object does not hash to $commit"
90 # The tree id is the first line of the commit object. A commit-graph must
91 # not be able to aim the archive at a different tree than that line.
92 line=$(git -C "$repo" -c core.commitGraph=false -c core.hooksPath=/dev/null -c core.fsmonitor= \
93 cat-file commit "$commit" | awk 'NR==1 { print; exit }') || prepare_fail "cannot read $label commit $commit"
94 tree=${line#tree }
95 [ "$line" = "tree $tree" ] && full_commit "$tree" || prepare_fail "$label commit $commit has no literal root tree"
96 got=$(git -C "$repo" -c core.commitGraph=false -c core.hooksPath=/dev/null -c core.fsmonitor= \
97 cat-file tree "$tree" 2>/dev/null | git hash-object -t tree --stdin 2>/dev/null) || prepare_fail "cannot re-hash $label tree $tree"
98 [ "$got" = "$tree" ] || prepare_fail "$label tree object does not hash to $tree"
99 git -C "$repo" -c core.commitGraph=false -c core.hooksPath=/dev/null -c core.fsmonitor= \
100 fsck --strict --no-dangling "$commit" >> "$BUILD_LOG" 2>&1 || prepare_fail "$label object closure fails git fsck"
101 printf '%s\n' "$tree"
102}
103
104TMP_BASE=${TMPDIR:-/tmp}
105plain_path "$TMP_BASE" || prepare_fail "TMPDIR contains characters outside [A-Za-z0-9/._+-]: $TMP_BASE"
106case "$TMP_BASE" in /*) ;; *) prepare_fail "TMPDIR must be absolute: $TMP_BASE";; esac
107[ "$TMP_BASE" != / ] || prepare_fail "TMPDIR may not be the filesystem root"
108BUILD_WORK=$(mktemp -d "$TMP_BASE/omega-r15-build.XXXXXX") || prepare_fail "cannot create private build directory"
109cleanup_build() {
110 case "$BUILD_WORK" in
111 "$TMP_BASE"/omega-r15-build.*)
112 chmod -R u+w "$BUILD_WORK" 2>/dev/null
113 rm -rf -- "$BUILD_WORK"
114 ;;
115 *) echo "r15_qualify: refusing to remove unexpected build path $BUILD_WORK" >&2 ;;
116 esac
117}
118RESTORE_NEEDED=false
119on_exit() {
120 [ "$RESTORE_NEEDED" != true ] || restore
121 cleanup_build
122}
123trap on_exit EXIT
124trap 'say "stopped by a signal"; exit 130' INT TERM HUP
125
126PHYSICS_LOCK=$(git show "$COMMIT:physics.lock" 2>/dev/null | tr -d '[:space:]')
127AIENOS_LOCK=$(git show "$COMMIT:aienos.lock" 2>/dev/null | tr -d '[:space:]')
128PHYSICS_INPUT=${PHYSICS_DIR:-$HERE/../physics}
129PHYSICS_REPO=$(git -C "$PHYSICS_INPUT" rev-parse --show-toplevel 2>/dev/null) || prepare_fail "PHYSICS_DIR must name a Git repository holding $PHYSICS_LOCK"
130if [ -n "${AIENOS_LOCK_REPO:-}" ]; then
131 AIENOS_INPUT=$AIENOS_LOCK_REPO
132elif [ -n "${AIENOS_R7_DIR:-}" ]; then
133 AIENOS_INPUT=$AIENOS_R7_DIR
134else
135 AIENOS_INPUT=$HERE/../aienos-argus-cap
136fi
137AIENOS_REPO=$(git -C "$AIENOS_INPUT" rev-parse --show-toplevel 2>/dev/null) || prepare_fail "AIENOS source must name a Git repository holding $AIENOS_LOCK"
138for path in "$HERE" "$PHYSICS_REPO" "$AIENOS_REPO" "$BUILD_WORK"; do
139 plain_path "$path" || prepare_fail "build path contains characters outside [A-Za-z0-9/._+-]: $path"
140done
141
142OMEGA_TREE=$(verify_commit "$HERE" "$COMMIT" omega) || exit 2
143PHYSICS_TREE=$(verify_commit "$PHYSICS_REPO" "$PHYSICS_LOCK" physics) || exit 2
144AIENOS_TREE=$(verify_commit "$AIENOS_REPO" "$AIENOS_LOCK" aienos) || exit 2
145
146CANDIDATE_TAR=$BUILD_WORK/omega.tar
147PHYSICS_TAR=$BUILD_WORK/physics.tar
148AIENOS_TAR=$BUILD_WORK/aienos-capability.tar
149# Archive the tree id taken from the commit object, not a peeled rev-parse.
150git -C "$HERE" -c core.commitGraph=false -c core.hooksPath=/dev/null -c core.fsmonitor= \
151 archive --format=tar "$OMEGA_TREE" > "$CANDIDATE_TAR" || prepare_fail "cannot archive omega candidate $COMMIT"
152git -C "$PHYSICS_REPO" -c core.commitGraph=false -c core.hooksPath=/dev/null -c core.fsmonitor= \
153 archive --format=tar "$PHYSICS_TREE" > "$PHYSICS_TAR" || prepare_fail "cannot archive physics commit $PHYSICS_LOCK"
154git -C "$AIENOS_REPO" -c core.commitGraph=false -c core.hooksPath=/dev/null -c core.fsmonitor= \
155 archive --format=tar "$AIENOS_TREE" native/capability > "$AIENOS_TAR" || prepare_fail "cannot archive aienos capability source at $AIENOS_LOCK"
156CANDIDATE_ARCHIVE_SHA=$(sha_file "$CANDIDATE_TAR") || prepare_fail "cannot hash omega candidate archive"
157PHYSICS_ARCHIVE_SHA=$(sha_file "$PHYSICS_TAR") || prepare_fail "cannot hash physics archive"
158AIENOS_ARCHIVE_SHA=$(sha_file "$AIENOS_TAR") || prepare_fail "cannot hash aienos archive"
159
160CANDIDATE_SRC=$BUILD_WORK/omega
161PHYSICS_SRC=$BUILD_WORK/physics
162BUILD_OUT=$BUILD_WORK/out
163mkdir "$CANDIDATE_SRC" "$PHYSICS_SRC" "$BUILD_OUT" || prepare_fail "cannot create isolated source/build directories"
164tar -xf "$CANDIDATE_TAR" -C "$CANDIDATE_SRC" || prepare_fail "cannot extract omega candidate archive"
165tar -xf "$PHYSICS_TAR" -C "$PHYSICS_SRC" || prepare_fail "cannot extract physics archive"
166
167CC_NAME=${CC:-gcc}
168case "$CC_NAME" in ''|*[!A-Za-z0-9_./+-]*) prepare_fail "CC must be one plain executable name or path";; esac
169CC_PATH=$(command -v -- "$CC_NAME" 2>/dev/null) || prepare_fail "compiler $CC_NAME is unavailable"
170CC_PATH=$(readlink -f "$CC_PATH") || prepare_fail "cannot resolve compiler $CC_NAME"
171MAKE_PATH=$(command -v -- make 2>/dev/null) || prepare_fail "make is unavailable"
172MAKE_PATH=$(readlink -f "$MAKE_PATH") || prepare_fail "cannot resolve make"
173plain_path "$CC_PATH" || prepare_fail "compiler path contains characters outside [A-Za-z0-9/._+-]: $CC_PATH"
174plain_path "$MAKE_PATH" || prepare_fail "make path contains characters outside [A-Za-z0-9/._+-]: $MAKE_PATH"
175CC_SHA=$(sha_file "$CC_PATH") || prepare_fail "cannot hash compiler $CC_PATH"
176MAKE_SHA=$(sha_file "$MAKE_PATH") || prepare_fail "cannot hash make $MAKE_PATH"
177CC_VERSION_ALL=$("$CC_PATH" --version 2>/dev/null) || prepare_fail "cannot read compiler version"
178CC_VERSION=${CC_VERSION_ALL%%$'\n'*}
179MAKE_VERSION_ALL=$("$MAKE_PATH" --version 2>/dev/null) || prepare_fail "cannot read make version"
180MAKE_VERSION=${MAKE_VERSION_ALL%%$'\n'*}
181BUILD_COMMAND="env -u build overrides make -C <omega-archive> OUT_DIR=<private> PHYSICS_DIR=<physics-archive> PHYSICS_LOCK_CHECK=0 AIENOS_LOCK_REPO=<verified-repository> CC=$CC_PATH r15-perf-$MODE"
182printf 'candidate=%s archive_sha256=%s\nphysics=%s archive_sha256=%s\naienos=%s capability_archive_sha256=%s\ncommand=%s\n' \
183 "$COMMIT" "$CANDIDATE_ARCHIVE_SHA" "$PHYSICS_LOCK" "$PHYSICS_ARCHIVE_SHA" \
184 "$AIENOS_LOCK" "$AIENOS_ARCHIVE_SHA" "$BUILD_COMMAND" >> "$BUILD_LOG"
185if ! env -u MAKEFLAGS -u MFLAGS -u MAKELEVEL -u CC -u CFLAGS -u CPPFLAGS -u LDFLAGS \
186 -u CPATH -u C_INCLUDE_PATH -u CPLUS_INCLUDE_PATH -u LIBRARY_PATH \
187 -u GCC_EXEC_PREFIX -u COMPILER_PATH -u LD_PRELOAD \
188 GIT_NO_REPLACE_OBJECTS=1 GIT_GRAFT_FILE=/nonexistent/r15-qualify-grafts \
189 "CC=$CC_PATH" \
190 "$MAKE_PATH" -C "$CANDIDATE_SRC" "OUT_DIR=$BUILD_OUT" "PHYSICS_DIR=$PHYSICS_SRC" \
191 PHYSICS_LOCK_CHECK=0 "AIENOS_LOCK_REPO=$AIENOS_REPO" "r15-perf-$MODE" \
192 >> "$BUILD_LOG" 2>&1; then
193 prepare_fail "isolated make r15-perf-$MODE failed (see build.log)"
194fi
195[ "$(sha_file "$CC_PATH")" = "$CC_SHA" ] || prepare_fail "compiler changed during the isolated build"
196[ "$(sha_file "$MAKE_PATH")" = "$MAKE_SHA" ] || prepare_fail "make changed during the isolated build"
197
198BUILT_B=$BUILD_OUT/rx_r15_perf_$MODE
199BUILT_BND=$BUILD_OUT/rx_r15_perf_${MODE}_nodigest
200BUILT_RED=$BUILD_OUT/r15_reduce
201for f in "$BUILT_B" "$BUILT_BND" "$BUILT_RED"; do
202 [ -x "$f" ] || prepare_fail "isolated build did not produce executable $f"
203done
204BIN_DIR=$OUT/bin
205mkdir "$BIN_DIR" || prepare_fail "cannot create sealed binary directory"
206B=$BIN_DIR/rx_r15_perf_$MODE
207BND=$BIN_DIR/rx_r15_perf_${MODE}_nodigest
208RED=$BIN_DIR/r15_reduce
209cp -- "$BUILT_B" "$B" || prepare_fail "cannot stage trial binary"
210cp -- "$BUILT_BND" "$BND" || prepare_fail "cannot stage nodigest binary"
211cp -- "$BUILT_RED" "$RED" || prepare_fail "cannot stage reducer"
212chmod 0555 "$B" "$BND" "$RED" "$BIN_DIR" || prepare_fail "cannot make staged artifacts read-only"
213TRIAL_SHA=$(sha_file "$B") || prepare_fail "cannot hash staged trial binary"
214NODIGEST_SHA=$(sha_file "$BND") || prepare_fail "cannot hash staged nodigest binary"
215REDUCER_SHA=$(sha_file "$RED") || prepare_fail "cannot hash staged reducer"
216[ "$TRIAL_SHA" = "$(sha_file "$BUILT_B")" ] || prepare_fail "staged trial binary differs from isolated build output"
217[ "$NODIGEST_SHA" = "$(sha_file "$BUILT_BND")" ] || prepare_fail "staged nodigest binary differs from isolated build output"
218[ "$REDUCER_SHA" = "$(sha_file "$BUILT_RED")" ] || prepare_fail "staged reducer differs from isolated build output"
219BUILD_LOG_SHA=$(sha_file "$BUILD_LOG") || prepare_fail "cannot hash build log"
220{
221 printf '{"schema":"AIEN_RX_R15_BUILD_PROVENANCE_V1","candidate_commit":%s,' "$(jstr "$COMMIT")"
222 printf '"candidate_archive_sha256":%s,"physics_commit":%s,"physics_archive_sha256":%s,' \
223 "$(jstr "$CANDIDATE_ARCHIVE_SHA")" "$(jstr "$PHYSICS_LOCK")" "$(jstr "$PHYSICS_ARCHIVE_SHA")"
224 printf '"aienos_commit":%s,"aienos_archive_sha256":%s,"build_command":%s,' \
225 "$(jstr "$AIENOS_LOCK")" "$(jstr "$AIENOS_ARCHIVE_SHA")" "$(jstr "$BUILD_COMMAND")"
226 printf '"compiler_path":%s,"compiler_sha256":%s,"compiler_version":%s,' \
227 "$(jstr "$CC_PATH")" "$(jstr "$CC_SHA")" "$(jstr "$CC_VERSION")"
228 printf '"make_path":%s,"make_sha256":%s,"make_version":%s,"build_log_sha256":%s,' \
229 "$(jstr "$MAKE_PATH")" "$(jstr "$MAKE_SHA")" "$(jstr "$MAKE_VERSION")" "$(jstr "$BUILD_LOG_SHA")"
230 printf '"binaries":{"trial":%s,"nodigest":%s,"reducer":%s}}\n' \
231 "$(jstr "$TRIAL_SHA")" "$(jstr "$NODIGEST_SHA")" "$(jstr "$REDUCER_SHA")"
232} > "$OUT/build-provenance.json"
233
234# ---- kernel.perf_event_paranoid 4 -> 1 -> 4 (C1 item 7) ---------------------
235PARANOID_BEFORE=$(cat /proc/sys/kernel/perf_event_paranoid)
236STATE=$CANDIDATE_SRC/tools/r15_machine_state.sh
237restore() {
238 "$STATE" stop "$OUT" 2>/dev/null
239 sudo -n sysctl -q kernel.perf_event_paranoid=4 >/dev/null 2>&1
240 PARANOID_RESTORED=$(cat /proc/sys/kernel/perf_event_paranoid)
241 echo "$PARANOID_RESTORED" > "$OUT/paranoid-restored.txt"
242 if [ "$PARANOID_RESTORED" != 4 ]; then say "WARNING: perf_event_paranoid did not return to 4 (reads $PARANOID_RESTORED)"; fi
243}
244RESTORE_NEEDED=true
245sudo -n sysctl -q kernel.perf_event_paranoid=1 >/dev/null 2>&1
246PARANOID_DURING=$(cat /proc/sys/kernel/perf_event_paranoid)
247
248# ---- machine.json (§13) ------------------------------------------------------
249midrs=$(for c in /sys/devices/system/cpu/cpu[0-9]*; do
250 printf '%s:%s ' "${c##*cpu}" "$(cat "$c/regs/identification/midr_el1" 2>/dev/null)"; done)
251govs=$(for c in /sys/devices/system/cpu/cpu[0-9]*; do
252 printf '%s:%s:%s ' "${c##*cpu}" "$(cat "$c/cpufreq/scaling_governor" 2>/dev/null)" \
253 "$(cat "$c/cpufreq/scaling_cur_freq" 2>/dev/null)"; done)
254temps=$(cat /sys/class/thermal/thermal_zone*/temp 2>/dev/null | tr '\n' ' ')
255spbm_ver=$(cat /sys/module/aien_spbm_readonly/srcversion 2>/dev/null)
256top=$(ps -eo pcpu,comm --sort=-pcpu | sed -n 2,11p | awk '{printf "%s:%s ", $2, $1}')
257servers=$(ps -eo comm,args | grep -i -E 'astrosage|lm-studio|lmstudio|llama|vllm|ollama' | grep -v grep | awk '{print $1}' | sort -u | tr '\n' ' ')
258aienos=$AIENOS_LOCK
259physics=$PHYSICS_LOCK
260{
261 printf '{"run_id":%s,"mode":%s,"candidate_commit":%s,"tree_dirty":%s,' \
262 "$(jstr "$RUN_ID")" "$(jstr "$MODE")" "$(jstr "$COMMIT")" "$DIRTY"
263 printf '"hostname":%s,"machine_id_sha256":%s,"kernel":%s,' \
264 "$(jstr "$(hostname)")" "$(jstr "$(sha256sum /etc/machine-id | cut -d' ' -f1)")" "$(jstr "$(uname -r)")"
265 printf '"midr":%s,"governor_freq":%s,"boost":%s,"mem_kb":%s,' \
266 "$(jstr "$midrs")" "$(jstr "$govs")" "$(jstr "$(cat /sys/devices/system/cpu/cpufreq/boost 2>/dev/null)")" \
267 "$(jstr "$(awk '/MemTotal/{print $2}' /proc/meminfo)")"
268 printf '"gpu":%s,"secure_boot":%s,"lockdown":%s,' \
269 "$(jstr "$(nvidia-smi --query-gpu=name,pci.bus_id,driver_version,temperature.gpu --format=csv,noheader 2>/dev/null)")" \
270 "$(jstr "$(mokutil --sb-state 2>/dev/null)")" "$(jstr "$(cat /sys/kernel/security/lockdown 2>/dev/null)")"
271 printf '"perf_event_paranoid":{"original":%s,"qualification":%s,"restored":"see paranoid-restored.txt"},' \
272 "$PARANOID_BEFORE" "$PARANOID_DURING"
273 printf '"thermal_mc":%s,"spbm_reader_srcversion":%s,"loadavg":%s,"top_cpu":%s,"model_servers":%s,' \
274 "$(jstr "$temps")" "$(jstr "$spbm_ver")" "$(jstr "$(cat /proc/loadavg)")" "$(jstr "$top")" "$(jstr "$servers")"
275 printf '"aienos_commit":%s,"physics_commit":%s,"compiler":%s,' \
276 "$(jstr "$aienos")" "$(jstr "$physics")" \
277 "$(jstr "$CC_VERSION")"
278 printf '"binaries":{"trial":%s,"nodigest":%s,"reducer":%s},' \
279 "$(jstr "$TRIAL_SHA")" "$(jstr "$NODIGEST_SHA")" "$(jstr "$REDUCER_SHA")"
280 printf '"reducer_source_sha256":%s,"rounds":%s,"l1_runs":%s,"l2_runs":%s}\n' \
281 "$(jstr "$(sha_file "$CANDIDATE_SRC/tools/r15_reduce.om")")" "$ROUNDS" "$L1RUNS" "$L2RUNS"
282} > "$OUT/machine.json"
283ps -eo pid,pcpu,rss,comm --sort=-pcpu > "$OUT/processes-before.txt"
284[ "$PARANOID_DURING" = 1 ] || say "WARNING: could not set perf_event_paranoid to 1 (reads $PARANOID_DURING); memory-traffic counters will be missing"
285[ -n "$servers" ] && say "WARNING: a model server looks resident: $servers (spec §9 says nothing heavy may run)"
286[ "$DIRTY" = true ] && say "NOTE: the working tree has uncommitted changes, so this run is not candidate-bound"
287
288# ---- instrument preflight (spec §7, §17 C2) ----------------------------------
289# Package energy is a mandatory metric (17) and G9's input. If the signed SPBM
290# reader is absent or returns invalid samples, the run cannot PASS, so stop
291# before consuming an acceptance window. This is BLOCKED_INSTRUMENT, not a
292# measured FAIL: no trial runs, no summary and no receipt is written.
293# planned run length for the counter-headroom check: the full default run (12/5/5) took 2068 s (CAND-1 window 2),
294# 2400 s with margin; scaled by the number of rounds and level runs. Override with R15_PLANNED_RUN_SECONDS.
295export R15_PLANNED_RUN_SECONDS=${R15_PLANNED_RUN_SECONDS:-$(( 2400 * (ROUNDS + L1RUNS + L2RUNS) / 22 + 1 ))}
296if [ "$MODE" = silicon ]; then
297 if ! ENERGY_MSG=$("$STATE" energy-preflight); then
298 say "BLOCKED_INSTRUMENT: $ENERGY_MSG"
299 printf 'BLOCKED_INSTRUMENT\n%s\n' "$ENERGY_MSG" > "$OUT/instrument-unavailable.txt"
300 echo instrument-unavailable > "$OUT/done"
301 # the blocked attempt stays on disk as evidence, never deleted; seal it
302 ( cd "$OUT" && find . -type f ! -name SHA256SUMS -printf "%P\n" | LC_ALL=C sort | xargs sha256sum > SHA256SUMS )
303 exit 4
304 fi
305 say "$ENERGY_MSG"
306fi
307
308# ---- machine physical state (§18 C3, observability only) --------------------
309# The preflight stops the run before any trial if the X925 does not sustain
310# its clock (the power-limited state seen on 2026-09-28). After that nothing
311# stops, drops or reruns a trial; the state is only recorded.
312if [ "$MODE" = silicon ]; then
313 say "machine-state preflight: 20 s sustained on one X925 core"
314 if ! "$STATE" preflight "$OUT"; then
315 say "MACHINE-STATE FAILURE: the X925 did not sustain its clock (preflight.txt); stopped before any trial"
316 echo machine-state-failure > "$OUT/done"
317 exit 3
318 fi
319 say "machine-state preflight ok: $(grep median "$OUT/preflight.txt")"
320fi
321"$STATE" start "$OUT"
322
323say "R15 $MODE run $RUN_ID started: $ROUNDS rounds, $L1RUNS Level-1 runs, $L2RUNS Level-2 runs"
324FAILED=0
325integrity_fail() { # <file> <expected> <phase>
326 local file=$1 expected=$2 phase=$3 actual
327 actual=$(sha_file "$file" 2>/dev/null)
328 say "EXECUTABLE INTEGRITY FAILURE: $(basename "$file") $phase expected $expected got ${actual:-unreadable}"
329 printf 'executable=%s\nphase=%s\nexpected_sha256=%s\nactual_sha256=%s\n' \
330 "$(basename "$file")" "$phase" "$expected" "${actual:-unreadable}" > "$OUT/executable-integrity-failure.txt"
331 exit 5
332}
333guard_binary() { # <file> <expected> <phase>
334 local actual
335 actual=$(sha_file "$1" 2>/dev/null) || integrity_fail "$1" "$2" "$3"
336 [ "$actual" = "$2" ] || integrity_fail "$1" "$2" "$3"
337}
338expected_binary() {
339 case "$1" in
340 "$B") printf '%s\n' "$TRIAL_SHA" ;;
341 "$BND") printf '%s\n' "$NODIGEST_SHA" ;;
342 "$RED") printf '%s\n' "$REDUCER_SHA" ;;
343 *) integrity_fail "$1" unknown "unregistered executable" ;;
344 esac
345}
346one() { # <label> <file> <binary> <args...>
347 local label=$1 file=$2 bin=$3
348 shift 3
349 local t0=$SECONDS expected
350 expected=$(expected_binary "$bin")
351 guard_binary "$bin" "$expected" "before $file"
352 "$STATE" mark "$OUT" "begin $file"
353 "$bin" "$@" "$OUT/$file" >> "$OUT/stderr.log" 2>&1
354 local rc=$?
355 guard_binary "$bin" "$expected" "after $file"
356 "$STATE" mark "$OUT" "end $file exit $rc"
357 echo "$file exit $rc seconds $((SECONDS - t0))" >> "$OUT/runs.txt"
358 if [ $rc -ne 0 ]; then FAILED=$((FAILED + 1)); say "$label FAILED (exit $rc); recorded, not rerun"; else say "$label ok ($((SECONDS - t0)) s)"; fi
359}
360
361# ---- W-EPISODE: balanced Latin square over 4 configurations (§10) ----------
362SQ=("0 1 3 2" "1 2 0 3" "2 3 1 0" "3 0 2 1")
363NAMES=(RES4 RES1 SEQ ND)
364for r in $(seq 1 "$ROUNDS"); do
365 for i in ${SQ[$(( (r - 1) % 4 ))]}; do
366 c=${NAMES[$i]}
367 if [ "$c" = ND ]; then
368 one "round $r RES-1-NODIGEST trial" "trial-RES1ND-$(printf %02d "$r").jsonl" "$BND" trial RES1 "$RUN_ID" "$r"
369 else
370 one "round $r $c trial" "trial-$c-$(printf %02d "$r").jsonl" "$B" trial "$c" "$RUN_ID" "$r"
371 fi
372 done
373done
374
375# ---- Level 1 ------------------------------------------------------------------
376for k in $(seq 1 "$L1RUNS"); do
377 for m in A C D E G W0 W1; do
378 if [ $((k % 2)) = 1 ]; then order="RES1 SEQ"; else order="SEQ RES1"; fi
379 for c in $order; do
380 one "Level 1 $m $c run $k" "l1-$m-$c-$k.jsonl" "$B" l1 "$m" "$c" "$RUN_ID" "$k"
381 done
382 done
383 one "Level 1 B RES1 run $k" "l1-B-RES1-$k.jsonl" "$B" l1 B RES1 "$RUN_ID" "$k"
384done
385
386# ---- Level 2 ------------------------------------------------------------------
387for k in $(seq 1 "$L2RUNS"); do
388 if [ $((k % 2)) = 1 ]; then order="RES1 SEQ"; else order="SEQ RES1"; fi
389 for c in $order; do one "Level 2 $c run $k" "l2-$c-$k.jsonl" "$B" l2 "$c" "$RUN_ID" "$k"; done
390done
391
392ps -eo pid,pcpu,rss,comm --sort=-pcpu > "$OUT/processes-after.txt"
393restore
394RESTORE_NEEDED=false
395(cd "$OUT" && sha256sum ./*.jsonl machine.json machine-state.ndjson machine-perf.csv machine-state-marks.txt $([ -f preflight.txt ] && echo preflight.txt) 2>/dev/null | sed 's| \./| |' > SHA256SUMS)
396guard_binary "$RED" "$REDUCER_SHA" "before reduction"
397"$RED" "$OUT" "$OUT/summary.json" > "$OUT/reduce.log" 2>&1
398guard_binary "$RED" "$REDUCER_SHA" "after reduction"
399
400# The final binding is separate from the reducer's raw-input manifest: it is
401# written only after every executable use and then sealed with the binaries,
402# build log and build provenance in PROVENANCE_SHA256SUMS.
403guard_binary "$B" "$TRIAL_SHA" "at final seal"
404guard_binary "$BND" "$NODIGEST_SHA" "at final seal"
405guard_binary "$RED" "$REDUCER_SHA" "at final seal"
406FINAL_HEAD=$(git rev-parse HEAD 2>/dev/null) || FINAL_HEAD=unreadable
407if FINAL_STATUS=$(git status --porcelain --untracked-files=no 2>/dev/null); then
408 FINAL_DIRTY=$([ -n "$FINAL_STATUS" ] && echo true || echo false)
409else
410 FINAL_DIRTY=true
411fi
412HEAD_UNCHANGED=false; [ "$FINAL_HEAD" = "$COMMIT" ] && HEAD_UNCHANGED=true
413{
414 printf '{"schema":"AIEN_RX_R15_FINAL_STATE_V1","candidate_commit":%s,"final_head":%s,' \
415 "$(jstr "$COMMIT")" "$(jstr "$FINAL_HEAD")"
416 printf '"tracked_tree_dirty":%s,"head_unchanged":%s,"binaries_unchanged":true,' \
417 "$FINAL_DIRTY" "$HEAD_UNCHANGED"
418 printf '"binaries":{"trial":%s,"nodigest":%s,"reducer":%s}}\n' \
419 "$(jstr "$TRIAL_SHA")" "$(jstr "$NODIGEST_SHA")" "$(jstr "$REDUCER_SHA")"
420} > "$OUT/final-state.json"
421(cd "$OUT" && sha256sum build.log build-provenance.json final-state.json \
422 "bin/$(basename "$B")" "bin/$(basename "$BND")" "bin/$(basename "$RED")" > PROVENANCE_SHA256SUMS) \
423 || { say "failed to seal build provenance"; exit 5; }
424say "finished: $FAILED process(es) failed; $(grep -c PASS "$OUT/reduce.log") of 16 gates pass (see summary.json)"
425tail -1 "$OUT/reduce.log" >> "$LOG"
426echo done > "$OUT/done"