diff --git a/.crumb b/.crumb index a7ee21cee..61248e065 100644 --- a/.crumb +++ b/.crumb @@ -52,7 +52,7 @@ }, { "digest": "6e14289e56c71da98a060589cfd4c23818169b62a9d030c8f76dc4c3c933bac5", - "last_commit": "ca12ad5", + "last_commit": "a386d65", "name": "docs", "purpose": "Directory docs. Purpose not yet described by a human." }, @@ -64,7 +64,7 @@ }, { "digest": "c5c1674600955e86dbd99a603d76ef472cd3742a1e8ed239a442fd0246f10d56", - "last_commit": "30c31b3", + "last_commit": "a386d65", "name": "mk", "purpose": "Directory mk. Purpose not yet described by a human." }, @@ -93,27 +93,27 @@ "purpose": "Directory src. Purpose not yet described by a human." }, { - "digest": "2154f18449ff36ac609f471d4316152cbacdf58e903410e55e59170d9a401aac", - "last_commit": "3822a0d", + "digest": "3d15ce0a4e66566f1d0e5ba22d84982aa0881dacf98732c2fa56b935406a8751", + "last_commit": "ec3659f", "name": "tests", "purpose": "Directory tests. Purpose not yet described by a human." }, { - "digest": "473ff59af294d59c6d7239ba756b3815b61abf89b620a17c05a2a1279a9591ee", - "last_commit": "30c31b3", + "digest": "5844e06455eea3780afb1dd3b3351ace41e1648a721491c692783c057fd2479d", + "last_commit": "ec3659f", "name": "tools", "purpose": "Directory tools. Purpose not yet described by a human." } ], - "children_root": "sha256:8aa5fccd75ff8f002572785f604f972739216c0279033de45fac1178c9c5745b", - "compiled_at": "2026-10-05T14:17:17Z", + "children_root": "sha256:41dab9e8a3aa8db7675d36d6d394395b5bfeb9ff64e2212bc5a2b991134cf881", + "compiled_at": "2026-10-05T15:03:46Z", "compiler": "crumb-compile/1", - "digest": "sha256:71c3ad20fbae4d1ac2ff02c8a3959691bf448033bf92ab3029c3fbccbf0bdc1c", + "digest": "sha256:712e4a8b0cdc4672bb315e5fbe799cbf24ca9eda6b0011cc2a706f7b43f8a5d4", "evidence_files": 2749, "evidence_root": "sha256:c6e60c5b2803cfddf6a1fed130c92066d285ddb68aeea0bc65d58668cf139dfc", - "files": 4538, - "generated_at_commit": "2275438a645b03cc09c37d51f104fbb8283046f4", - "source_tree": "sha256:d023816cdf01afe757fe2fb0cd23cea9842b644be6ffa0cf13eb4e881b9ccf9b" + "files": 4539, + "generated_at_commit": "ec3659fd8cda2fc4b16dbd4e729dae172ccdae6a", + "source_tree": "sha256:63721a48df45ecf4fe43294a06c2c5004307f25869633b0d2170af4ed96da90f" }, "owner_lane": "", "provenance": { diff --git a/Makefile b/Makefile index 4c4d51fcd..8f6b4d9e9 100644 --- a/Makefile +++ b/Makefile @@ -818,6 +818,7 @@ test-r15-receipt: .PHONY: test-r15-preflight test-r15-preflight: sh tests/r15_preflight_test.sh + sh tests/r15_energy_preflight_test.sh # R16-G2 code-search gate (spec/r16-orchestrator-retirement.md). Host-only C tool, # seconds, no network. Scans the five repos (paths from R16_REPO_OMEGA, diff --git a/tests/.crumb b/tests/.crumb index beb9c1e2e..68ff3fed0 100644 --- a/tests/.crumb +++ b/tests/.crumb @@ -284,14 +284,14 @@ } ], "children_root": "sha256:215c9654ba0e4f579f3f1c7f09b3ecdacc7c922de1af68565150b55fedf9729a", - "compiled_at": "2026-10-05T13:14:18Z", + "compiled_at": "2026-10-05T15:03:46Z", "compiler": "crumb-compile/1", - "digest": "sha256:2154f18449ff36ac609f471d4316152cbacdf58e903410e55e59170d9a401aac", + "digest": "sha256:3d15ce0a4e66566f1d0e5ba22d84982aa0881dacf98732c2fa56b935406a8751", "evidence_files": 11, "evidence_root": "sha256:d5c1bceb4d57ef0eb42797c58801b5926509711614c3e38bb7105b6efa3942f2", - "files": 797, - "generated_at_commit": "0c406321c84491f9069d9d438e3f5a9896044869", - "source_tree": "sha256:182e14a0ed6200893a29f2f5d42fa464b2a3c002ad4c584a7ceed83bd972e084" + "files": 798, + "generated_at_commit": "ec3659fd8cda2fc4b16dbd4e729dae172ccdae6a", + "source_tree": "sha256:f346163d2500f3984cebb6ecc27c2e93787c92dd296ee676a8c443aa762cc425" }, "owner_lane": "", "provenance": { diff --git a/tests/r15_energy_preflight_test.sh b/tests/r15_energy_preflight_test.sh new file mode 100644 index 000000000..a4d640acd --- /dev/null +++ b/tests/r15_energy_preflight_test.sh @@ -0,0 +1,62 @@ +#!/bin/sh +# R15 energy-instrument preflight (tools/r15_machine_state.sh energy-preflight). +# Spec §7: if package energy cannot be read by the physical SPBM source, R15 +# cannot PASS. The preflight must refuse EARLY with INSTRUMENT_UNAVAILABLE +# (exit 5) whenever the reader would not satisfy r15_energy_open/r15_energy_read +# (tests/runtime/r15_measure.c): all 5 energy + 5 power labels exact, every +# energyN_input / energyN_overflow_raw / powerN_input readable, overflow 0, +# package energy advancing, counter headroom. The preflight reads its label +# tables from r15_measure.c, and this test builds its fixtures from the same +# tables, so the two cannot drift. Fixtures are fake hwmon trees in a temp dir. +cd "$(dirname "$0")/.." || exit 1 +T=tools/r15_machine_state.sh; fail=0; C=tests/runtime/r15_measure.c +W=$(mktemp -d) || exit 1; trap 'rm -rf "$W"' EXIT +export R15_ENERGY_PREFLIGHT_SLEEP=0 +EL=$(sed -n 's/^static const char \*const k_elabel\[[A-Z_0-9]*\] = {\(.*\)};/\1/p' $C | tr -d '" ' | tr , ' ') +PL=$(sed -n 's/^static const char \*const k_plabel\[[A-Z_0-9]*\] = {\(.*\)};/\1/p' $C | tr -d '" ' | tr , ' ') +[ "$(echo $EL | wc -w)" = 5 ] && [ "$(echo $PL | wc -w)" = 5 ] || { echo "[-] cannot read label tables from $C"; exit 1; } +check() { # name expected-exit expected-text + out=$(R15_HWMON_ROOT="$W/$1" "$T" energy-preflight 2>&1); rc=$? + if [ "$rc" = "$2" ] && printf '%s\n' "$out" | grep -Fq -- "$3"; then echo "[+] $1: exit $rc" + else echo "[-] $1: exit $rc (want $2), output: $out"; fail=$((fail + 1)); fi +} +mk() { # case: a complete, valid-looking reader tree (static counters) + H=$W/$1/hwmon1; mkdir -p "$W/$1/hwmon0" "$H"; echo acpitz > "$W/$1/hwmon0/name"; echo aien_spbm > "$H/name" + i=1; for l in $EL; do echo "$l" > "$H/energy${i}_label"; echo 5000000 > "$H/energy${i}_input"; echo 0 > "$H/energy${i}_overflow_raw"; i=$((i + 1)); done + i=1; for l in $PL; do echo "$l" > "$H/power${i}_label"; echo 20000000 > "$H/power${i}_input"; i=$((i + 1)); done +} +mkdir -p "$W/absent/hwmon0"; echo acpitz > "$W/absent/hwmon0/name"; check absent 5 "reason=absent" +mkdir -p "$W/empty"; check empty 5 "reason=absent" +mk substitute; echo nvml > "$W/substitute/hwmon1/name"; check substitute 5 "reason=absent" +mk zero; echo 0 > "$W/zero/hwmon1/energy1_input"; check zero 5 "reads 0 uJ" +mk unreadable; rm "$W/unreadable/hwmon1/energy1_input"; check unreadable 5 "energy1_input" +mk garbage; echo -22 > "$W/garbage/hwmon1/energy1_input"; check garbage 5 "not a number" +mk stuck; check stuck 5 "did not advance" +mk wrongelabel; echo gpm > "$W/wrongelabel/hwmon1/energy1_label"; check wrongelabel 5 "energy1_label" +mk wrongelabel4; echo x > "$W/wrongelabel4/hwmon1/energy4_label"; check wrongelabel4 5 "energy4_label" +mk wrongplabel; echo x > "$W/wrongplabel/hwmon1/power3_label"; check wrongplabel 5 "power3_label" +mk missingplabel; rm "$W/missingplabel/hwmon1/power5_label"; check missingplabel 5 "power5_label" +mk noovf; rm "$W/noovf/hwmon1/energy2_overflow_raw"; check noovf 5 "energy2_overflow_raw unreadable" +mk ovf1; echo 1 > "$W/ovf1/hwmon1/energy1_overflow_raw"; check ovf1 5 "energy1_overflow_raw is '1'" +mk ovf5; echo 1 > "$W/ovf5/hwmon1/energy5_overflow_raw"; check ovf5 5 "energy5_overflow_raw is '1'" +mk nopower; rm "$W/nopower/hwmon1/power2_input"; check nopower 5 "power2_input" +mk badpower; echo abc > "$W/badpower/hwmon1/power4_input"; check badpower 5 "power4_input" +mk noenergy3; rm "$W/noenergy3/hwmon1/energy3_input"; check noenergy3 5 "energy3_input" +mk lowhead; echo 4294967000000000 > "$W/lowhead/hwmon1/energy3_input"; check lowhead 5 "headroom" +# valid: package counter advances between samples +mk valid +( sleep 0.05; echo 100500 > "$W/valid/hwmon1/energy1_input"; sleep 0.1; echo 101000 > "$W/valid/hwmon1/energy1_input" ) & +echo 100000 > "$W/valid/hwmon1/energy1_input" +R15_ENERGY_PREFLIGHT_SLEEP=0.08 check valid 0 "energy preflight ok" +wait +# blocked run: receipt refuses (exit 4), writes nothing +R=$W/blocked; mkdir -p "$R"; printf 'BLOCKED_INSTRUMENT\nINSTRUMENT_UNAVAILABLE reason=absent\n' > "$R/instrument-unavailable.txt" +out=$(tools/r15_receipt.sh "$R" "$W/receipt-out" 2>&1); rc=$? +if [ "$rc" = 4 ] && printf '%s' "$out" | grep -Fq BLOCKED_INSTRUMENT && [ -z "$(ls "$W/receipt-out" 2>/dev/null)" ]; then + echo "[+] receipt refuses blocked run: exit $rc, nothing written" +else echo "[-] receipt on blocked run: exit $rc (want 4): $out"; fail=$((fail + 1)); fi +# the qualify path seals a blocked directory with SHA256SUMS (same command as r15_qualify.sh) +( cd "$R" && find . -type f ! -name SHA256SUMS -printf "%P\n" | LC_ALL=C sort | xargs sha256sum > SHA256SUMS ) +if ( cd "$R" && sha256sum -c SHA256SUMS >/dev/null 2>&1 ) && grep -q instrument-unavailable.txt "$R/SHA256SUMS"; then echo "[+] blocked dir sealed"; else echo "[-] seal"; fail=$((fail + 1)); fi +grep -q 'printf "%P\\n" | LC_ALL=C sort | xargs sha256sum > SHA256SUMS' tools/r15_qualify.sh && echo "[+] qualify.sh uses the same seal command" || { echo "[-] qualify.sh seal missing"; fail=$((fail + 1)); } +echo "failures $fail"; [ "$fail" = 0 ] diff --git a/tools/.crumb b/tools/.crumb index c18297be3..6e99b44b8 100644 --- a/tools/.crumb +++ b/tools/.crumb @@ -44,7 +44,7 @@ }, { "digest": "d4a6875be9942403548ed74e5f75e57bfe5824237dc985ae51813f42230d6134", - "last_commit": "30c31b3", + "last_commit": "a386d65", "name": "estimation", "purpose": "Directory tools/estimation. Purpose not yet described by a human." }, @@ -56,14 +56,14 @@ } ], "children_root": "sha256:3f9857a8b0946837f5556bcea0b4deaddd3a5aa3a60a31aa08ac917ac69d19f9", - "compiled_at": "2026-10-05T14:17:17Z", + "compiled_at": "2026-10-05T15:03:46Z", "compiler": "crumb-compile/1", - "digest": "sha256:473ff59af294d59c6d7239ba756b3815b61abf89b620a17c05a2a1279a9591ee", + "digest": "sha256:5844e06455eea3780afb1dd3b3351ace41e1648a721491c692783c057fd2479d", "evidence_files": 0, "evidence_root": "sha256:e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855", "files": 132, - "generated_at_commit": "2275438a645b03cc09c37d51f104fbb8283046f4", - "source_tree": "sha256:800b487949450814bb97a57bb235e1166de4df135cdc3c7401e76a6f2a4c8333" + "generated_at_commit": "ec3659fd8cda2fc4b16dbd4e729dae172ccdae6a", + "source_tree": "sha256:af007b489c00d3ca31e9389fa6b8fc2e66853381f6a676ca17212ffea46728df" }, "owner_lane": "", "provenance": { diff --git a/tools/r15_machine_state.sh b/tools/r15_machine_state.sh index b940cae60..8e14c4042 100755 --- a/tools/r15_machine_state.sh +++ b/tools/r15_machine_state.sh @@ -6,6 +6,8 @@ # # tools/r15_machine_state.sh preflight exit 0 if the X925 sustains # its clock, 3 if it does not +# tools/r15_machine_state.sh energy-preflight exit 0 if the SPBM package-energy +# reader gives valid samples, 5 if not # tools/r15_machine_state.sh start begin 1 s sampling # tools/r15_machine_state.sh mark # tools/r15_machine_state.sh stop end sampling, write summary @@ -37,6 +39,85 @@ preflight_ghz() { # preflight-parse : the preflight verdict on a recorded file (tests). if [ "${1:-}" = preflight-parse ]; then preflight_ghz "${2:?perf csv}"; exit $?; fi + +# energy_preflight: is the spec §7/§17 C2 package-energy reader present and +# returning valid samples? The binding source is the owner-key signed +# read-only SPBM reader (hwmon name "aien_spbm", energy1 labelled "pkg", the +# same match tests/runtime/r15_measure.c uses). Spec §7: "If package energy +# cannot be read by a physical hardware source at qualification time, metric +# 17 is incomplete and R15 cannot PASS. GPU-domain energy alone is not +# accepted", and §17 says GPM and NVML are "never substituted". So no other +# source is tried here. Exit 0 = reader valid; 5 = INSTRUMENT_UNAVAILABLE +# (reason=absent or reason=invalid). That is a blocked run, not a measured +# FAIL: no trial is started, no receipt is written. +# Env (tests): R15_HWMON_ROOT (default /sys/class/hwmon), R15_ENERGY_PREFLIGHT_SLEEP (0.1 s). +# The reader contract is read from tests/runtime/r15_measure.c itself (its +# k_elabel / k_plabel tables), so the preflight cannot drift from what +# r15_energy_open / r15_energy_read require: all 5 energy and 5 power labels +# exact; every energyN_input, energyN_overflow_raw and powerN_input readable +# as a non-negative integer. Beyond that the preflight also requires every +# overflow_raw to be 0 and package energy to advance over three samples. +# Counter headroom: spec §17 "A window is refused if any overflow flag is set, +# any counter decreases, or headroom is below the window length at a 600 W +# bound (sample.c)". The per-window refusal lives in research/m15/spbm/sample.c; +# the R15 harness and reducer reject overflow flags and decreases (energy_ok). +# This preflight adds the headroom check for R15_ENERGY_HEADROOM_SECONDS +# (default 300 s, longer than any single measured process) at 600 W. +R15_MEASURE_C=$(cd "$(dirname "$0")/.." && pwd)/tests/runtime/r15_measure.c +energy_preflight() { + local root=${R15_HWMON_ROOT:-/sys/class/hwmon} nap=${R15_ENERGY_PREFLIGHT_SLEEP:-0.1} + local hs=${R15_ENERGY_HEADROOM_SECONDS:-300} + local h="" d s v prev="" first="" n=0 i lab want + local el pl + el=$(sed -n 's/^static const char \*const k_elabel\[[A-Z_0-9]*\] = {\(.*\)};/\1/p' "$R15_MEASURE_C" | tr -d '" ') + pl=$(sed -n 's/^static const char \*const k_plabel\[[A-Z_0-9]*\] = {\(.*\)};/\1/p' "$R15_MEASURE_C" | tr -d '" ') + if [ "$(printf "%s" "$el" | awk -F, "{print NF}")" != 5 ] || [ "$(printf "%s" "$pl" | awk -F, "{print NF}")" != 5 ]; then + echo "INSTRUMENT_UNAVAILABLE reason=invalid: cannot read the 5+5 label tables from $R15_MEASURE_C" + return 5 + fi + for d in "$root"/hwmon*; do + [ "$(cat "$d/name" 2>/dev/null)" = aien_spbm ] && { h=$d; break; } + done + if [ -z "$h" ]; then + echo "INSTRUMENT_UNAVAILABLE reason=absent: no hwmon named aien_spbm (the signed SPBM reader is not loaded); package energy cannot be read, R15 cannot PASS (spec §7)" + return 5 + fi + for i in 1 2 3 4 5; do + want=$(printf '%s' "$el" | cut -d, -f$i); lab=$(cat "$h/energy${i}_label" 2>/dev/null) + [ "$lab" = "$want" ] || { echo "INSTRUMENT_UNAVAILABLE reason=invalid: energy${i}_label is '$lab', not $want"; return 5; } + want=$(printf '%s' "$pl" | cut -d, -f$i); lab=$(cat "$h/power${i}_label" 2>/dev/null) + [ "$lab" = "$want" ] || { echo "INSTRUMENT_UNAVAILABLE reason=invalid: power${i}_label is '$lab', not $want"; return 5; } + done + for s in 1 2 3; do + for i in 1 2 3 4 5; do + v=$(cat "$h/energy${i}_overflow_raw" 2>/dev/null) + case "$v" in 0) ;; '') echo "INSTRUMENT_UNAVAILABLE reason=invalid: energy${i}_overflow_raw unreadable"; return 5;; + *) echo "INSTRUMENT_UNAVAILABLE reason=invalid: energy${i}_overflow_raw is '$v' (must be 0)"; return 5;; esac + v=$(cat "$h/power${i}_input" 2>/dev/null) + case "$v" in ''|*[!0-9]*) echo "INSTRUMENT_UNAVAILABLE reason=invalid: power${i}_input unreadable or not a number ('$v')"; return 5;; esac + v=$(cat "$h/energy${i}_input" 2>/dev/null) + case "$v" in ''|*[!0-9]*) echo "INSTRUMENT_UNAVAILABLE reason=invalid: energy${i}_input sample $s unreadable or not a number ('$v')"; return 5;; esac + if [ "$i" = 1 ]; then + if [ "$v" -le 0 ]; then echo "INSTRUMENT_UNAVAILABLE reason=invalid: package sample $s reads $v uJ (a running counter is above zero)"; return 5; fi + if [ -n "$prev" ] && [ "$v" -lt "$prev" ]; then echo "INSTRUMENT_UNAVAILABLE reason=invalid: package counter went backwards ($prev -> $v uJ)"; return 5; fi + [ -z "$first" ] && first=$v + prev=$v; n=$((n + 1)) + fi + # headroom to the 32-bit mJ wrap (as uJ) at a 600 W bound over the window + if [ $((4294967295 * 1000 - v)) -lt $((hs * 600000000)) ]; then + echo "INSTRUMENT_UNAVAILABLE reason=invalid: energy${i} has less than ${hs} s of headroom before the counter wraps at 600 W (spec §17)"; return 5 + fi + done + [ "$s" -lt 3 ] && sleep "$nap" + done + if [ "$prev" -le "$first" ]; then + echo "INSTRUMENT_UNAVAILABLE reason=invalid: package counter did not advance over $n samples ($first uJ)" + return 5 + fi + echo "energy preflight ok: $h pkg $first -> $prev uJ over $n samples, 5 energy + 5 power labels verified" + return 0 +} +if [ "${1:-}" = energy-preflight ]; then energy_preflight; exit $?; fi CMD=${1:?preflight, start, mark or stop} OUT=${2:?out dir} mkdir -p "$OUT" diff --git a/tools/r15_qualify.sh b/tools/r15_qualify.sh index 9e83a9fce..48b569dc4 100755 --- a/tools/r15_qualify.sh +++ b/tools/r15_qualify.sh @@ -109,6 +109,23 @@ ps -eo pid,pcpu,rss,comm --sort=-pcpu > "$OUT/processes-before.txt" [ -n "$servers" ] && say "WARNING: a model server looks resident: $servers (spec §9 says nothing heavy may run)" [ "$DIRTY" = true ] && say "NOTE: the working tree has uncommitted changes, so this run is not candidate-bound" +# ---- instrument preflight (spec §7, §17 C2) ---------------------------------- +# Package energy is a mandatory metric (17) and G9's input. If the signed SPBM +# reader is absent or returns invalid samples, the run cannot PASS, so stop +# before consuming an acceptance window. This is BLOCKED_INSTRUMENT, not a +# measured FAIL: no trial runs, no summary and no receipt is written. +if [ "$MODE" = silicon ]; then + if ! ENERGY_MSG=$("$STATE" energy-preflight); then + say "BLOCKED_INSTRUMENT: $ENERGY_MSG" + printf 'BLOCKED_INSTRUMENT\n%s\n' "$ENERGY_MSG" > "$OUT/instrument-unavailable.txt" + echo instrument-unavailable > "$OUT/done" + # the blocked attempt stays on disk as evidence, never deleted; seal it + ( cd "$OUT" && find . -type f ! -name SHA256SUMS -printf "%P\n" | LC_ALL=C sort | xargs sha256sum > SHA256SUMS ) + exit 4 + fi + say "$ENERGY_MSG" +fi + # ---- machine physical state (§18 C3, observability only) -------------------- # The preflight stops the run before any trial if the X925 does not sustain # its clock (the power-limited state seen on 2026-09-28). After that nothing diff --git a/tools/r15_receipt.sh b/tools/r15_receipt.sh index dfb8cbd14..d0d964c37 100755 --- a/tools/r15_receipt.sh +++ b/tools/r15_receipt.sh @@ -44,6 +44,12 @@ RERUNS=${4:-} NOTES=${5:-} HERE=$(cd "$(dirname "$0")/.." && pwd) +# A run stopped by the instrument preflight is BLOCKED_INSTRUMENT: the run never +# measured anything, so there is nothing to receipt. Refuse, write nothing. +if [ -s "$RAW/instrument-unavailable.txt" ]; then + echo "r15_receipt: BLOCKED_INSTRUMENT: this run stopped before any trial (instrument unavailable); no receipt written. $(sed -n 2p "$RAW/instrument-unavailable.txt")" >&2 + exit 4 +fi SUM=$RAW/summary.json MACH=$RAW/machine.json for f in "$SUM" "$MACH" "$RAW/SHA256SUMS"; do