From ec3659fd8cda2fc4b16dbd4e729dae172ccdae6a Mon Sep 17 00:00:00 2001 From: Drake Stapleton Date: Mon, 5 Oct 2026 09:56:19 -0500 Subject: [PATCH 1/2] R15: stop before the window when the SPBM energy reader is missing or invalid (BLOCKED_INSTRUMENT) Window 2 of CAND-1 ran with no aien_spbm hwmon (all 58 processes recorded spbm=0), so G9 had 0 valid pairs and G16 missed metric 17. Spec section 7: without package energy from a physical source R15 cannot PASS, and GPU-domain energy is never substituted (C2). - r15_machine_state.sh energy-preflight: reader present (hwmon name aien_spbm, energy1 label pkg), no overflow flag, three numeric samples above zero that advance and never decrease. Exit 5 = INSTRUMENT_UNAVAILABLE (reason=absent|invalid). - r15_qualify.sh (silicon): runs it after machine.json and before the clock preflight; on exit 5 writes instrument-unavailable.txt, done=instrument-unavailable, exits 4. No trial runs, no summary, no PASS. - r15_receipt.sh: refuses such a run directory (exit 4) and writes no receipt, so it can never be mistaken for a measured FAIL or PASS. Gate semantics unchanged. - tests/r15_energy_preflight_test.sh (host only, fake hwmon trees): absent, empty, zero, unreadable, garbage, stuck, wrong label, overflow, substitute source -> refused; advancing counter -> passes; receipt refuses blocked run. Red on origin/main (11 failures), green here. Wired into make test-r15-preflight. Co-Authored-By: Claude Opus 5.5 --- .crumb | 22 +++++------ Makefile | 1 + tests/.crumb | 10 ++--- tests/r15_energy_preflight_test.sh | 60 ++++++++++++++++++++++++++++++ tools/.crumb | 10 ++--- tools/r15_machine_state.sh | 50 +++++++++++++++++++++++++ tools/r15_qualify.sh | 15 ++++++++ tools/r15_receipt.sh | 6 +++ 8 files changed, 153 insertions(+), 21 deletions(-) create mode 100644 tests/r15_energy_preflight_test.sh diff --git a/.crumb b/.crumb index a7ee21cee..b3d6bfd97 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", + "digest": "0dd887087aad1ba31bef855b4760172134c4a94a673dc940d7055ef88681d161", "last_commit": "3822a0d", "name": "tests", "purpose": "Directory tests. Purpose not yet described by a human." }, { - "digest": "473ff59af294d59c6d7239ba756b3815b61abf89b620a17c05a2a1279a9591ee", - "last_commit": "30c31b3", + "digest": "1a09a6c77f3da2f0c244cdbbc6d2e2ec3fd4908ade5b32a762db4e8b7899372f", + "last_commit": "a386d65", "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:7910d31ff39692b4dae41020e7ec3e6dd9c33041e9df1526cc0573f385808b13", + "compiled_at": "2026-10-05T14:55:30Z", "compiler": "crumb-compile/1", - "digest": "sha256:71c3ad20fbae4d1ac2ff02c8a3959691bf448033bf92ab3029c3fbccbf0bdc1c", + "digest": "sha256:6aac6d9faee310edf51bccf7c4298d5547f0a278e461c506c9b3c15e6fd008b0", "evidence_files": 2749, "evidence_root": "sha256:c6e60c5b2803cfddf6a1fed130c92066d285ddb68aeea0bc65d58668cf139dfc", - "files": 4538, - "generated_at_commit": "2275438a645b03cc09c37d51f104fbb8283046f4", - "source_tree": "sha256:d023816cdf01afe757fe2fb0cd23cea9842b644be6ffa0cf13eb4e881b9ccf9b" + "files": 4539, + "generated_at_commit": "a386d65ed193a02dccac444de3fd3224c96162b4", + "source_tree": "sha256:1e604db9c283e57175c287c8217b6d0de67eddaafeabdfb9e90897baf0c90f43" }, "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..9ce62981b 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-05T14:55:30Z", "compiler": "crumb-compile/1", - "digest": "sha256:2154f18449ff36ac609f471d4316152cbacdf58e903410e55e59170d9a401aac", + "digest": "sha256:0dd887087aad1ba31bef855b4760172134c4a94a673dc940d7055ef88681d161", "evidence_files": 11, "evidence_root": "sha256:d5c1bceb4d57ef0eb42797c58801b5926509711614c3e38bb7105b6efa3942f2", - "files": 797, - "generated_at_commit": "0c406321c84491f9069d9d438e3f5a9896044869", - "source_tree": "sha256:182e14a0ed6200893a29f2f5d42fa464b2a3c002ad4c584a7ceed83bd972e084" + "files": 798, + "generated_at_commit": "a386d65ed193a02dccac444de3fd3224c96162b4", + "source_tree": "sha256:89958e99dbb2820095288f778b832dedb3b398d9b1be0d9aafc3f3d0cee7fd99" }, "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..c9c044d31 --- /dev/null +++ b/tests/r15_energy_preflight_test.sh @@ -0,0 +1,60 @@ +#!/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) when the reader is absent or gives invalid samples, and pass (exit 0) +# when it gives a valid advancing counter. Fixtures are fake hwmon trees built +# in a temp dir; nothing touches the machine. The receipt writer must refuse a +# BLOCKED_INSTRUMENT run directory (exit 4) and write nothing. +cd "$(dirname "$0")/.." || exit 1 +T=tools/r15_machine_state.sh; fail=0 +W=$(mktemp -d) || exit 1; trap 'rm -rf "$W"' EXIT +export R15_ENERGY_PREFLIGHT_SLEEP=0 +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 name label first-value-file-content + mkdir -p "$W/$1/hwmon0" "$W/$1/hwmon1" + echo acpitz > "$W/$1/hwmon0/name" + echo "$2" > "$W/$1/hwmon1/name" + echo "$3" > "$W/$1/hwmon1/energy1_label" +} +# reader absent: only unrelated hwmon devices +mkdir -p "$W/absent/hwmon0"; echo acpitz > "$W/absent/hwmon0/name" +check absent 5 "INSTRUMENT_UNAVAILABLE reason=absent" +mkdir -p "$W/empty" +check empty 5 "INSTRUMENT_UNAVAILABLE reason=absent" +# reader present, invalid samples +mk zero aien_spbm pkg; echo 0 > "$W/zero/hwmon1/energy1_input" +check zero 5 "reason=invalid" +mk unreadable aien_spbm pkg # energy1_input missing (kernel returned an error) +check unreadable 5 "reason=invalid" +mk garbage aien_spbm pkg; echo "-22" > "$W/garbage/hwmon1/energy1_input" +check garbage 5 "reason=invalid" +mk stuck aien_spbm pkg; echo 5000000 > "$W/stuck/hwmon1/energy1_input" +check stuck 5 "counter did not advance" +mk wronglabel aien_spbm gpm; echo 5000000 > "$W/wronglabel/hwmon1/energy1_input" +check wronglabel 5 "energy1_label is not pkg" +mk overflow aien_spbm pkg; echo 5000000 > "$W/overflow/hwmon1/energy1_input"; echo 1 > "$W/overflow/hwmon1/energy1_overflow_raw" +check overflow 5 "overflow indicator" +# another energy source must not be accepted in place of the reader +mk substitute nvml pkg; echo 5000000 > "$W/substitute/hwmon1/energy1_input" +check substitute 5 "reason=absent" +# valid reader: a counter that advances between samples +mk valid aien_spbm pkg; echo 100000 > "$W/valid/hwmon1/energy1_input"; echo 0 > "$W/valid/hwmon1/energy1_overflow_raw" +# a static file cannot advance; emulate with a FIFO-free trick: a shell function is not +# possible across processes, so use sleep-based writer for the valid case +( sleep 0.05; echo 100500 > "$W/valid/hwmon1/energy1_input"; sleep 0.05; echo 101000 > "$W/valid/hwmon1/energy1_input" ) & +export R15_ENERGY_PREFLIGHT_SLEEP=0.08 +check valid 0 "energy preflight ok" +wait +export R15_ENERGY_PREFLIGHT_SLEEP=0 +# receipt writer: a blocked run is refused with its own exit code, no output written +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 +echo "failures $fail"; [ "$fail" = 0 ] diff --git a/tools/.crumb b/tools/.crumb index c18297be3..a15b6a84b 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-05T14:55:30Z", "compiler": "crumb-compile/1", - "digest": "sha256:473ff59af294d59c6d7239ba756b3815b61abf89b620a17c05a2a1279a9591ee", + "digest": "sha256:1a09a6c77f3da2f0c244cdbbc6d2e2ec3fd4908ade5b32a762db4e8b7899372f", "evidence_files": 0, "evidence_root": "sha256:e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855", "files": 132, - "generated_at_commit": "2275438a645b03cc09c37d51f104fbb8283046f4", - "source_tree": "sha256:800b487949450814bb97a57bb235e1166de4df135cdc3c7401e76a6f2a4c8333" + "generated_at_commit": "a386d65ed193a02dccac444de3fd3224c96162b4", + "source_tree": "sha256:cb9af1c273557a94532ee5e79118aa447a8af94dfdf07c6ba69aa1e9d55c42bc" }, "owner_lane": "", "provenance": { diff --git a/tools/r15_machine_state.sh b/tools/r15_machine_state.sh index b940cae60..34320be7a 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,54 @@ 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). +energy_preflight() { + local root=${R15_HWMON_ROOT:-/sys/class/hwmon} nap=${R15_ENERGY_PREFLIGHT_SLEEP:-0.1} + local h="" d s v prev="" first="" n=0 ov + 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 + if [ "$(cat "$h/energy1_label" 2>/dev/null)" != pkg ]; then + echo "INSTRUMENT_UNAVAILABLE reason=invalid: energy1_label is not pkg at $h" + return 5 + fi + ov=$(cat "$h/energy1_overflow_raw" 2>/dev/null) + if [ -n "$ov" ] && [ "$ov" != 0 ]; then + echo "INSTRUMENT_UNAVAILABLE reason=invalid: energy1 overflow indicator is $ov" + return 5 + fi + for s in 1 2 3; do + v=$(cat "$h/energy1_input" 2>/dev/null) + case "$v" in ''|*[!0-9]*) echo "INSTRUMENT_UNAVAILABLE reason=invalid: sample $s of energy1_input is not a number ('$v')"; return 5;; esac + if [ "$v" -le 0 ]; then echo "INSTRUMENT_UNAVAILABLE reason=invalid: sample $s reads $v uJ (a running package counter is above zero)"; return 5; fi + if [ -n "$prev" ] && [ "$v" -lt "$prev" ]; then echo "INSTRUMENT_UNAVAILABLE reason=invalid: counter went backwards ($prev -> $v uJ)"; return 5; fi + [ -z "$first" ] && first=$v + prev=$v; n=$((n + 1)) + [ "$s" -lt 3 ] && sleep "$nap" + done + if [ "$prev" -le "$first" ]; then + echo "INSTRUMENT_UNAVAILABLE reason=invalid: counter did not advance over $n samples ($first uJ)" + return 5 + fi + echo "energy preflight ok: $h pkg $first -> $prev uJ over $n samples" + 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..ead44d678 100755 --- a/tools/r15_qualify.sh +++ b/tools/r15_qualify.sh @@ -109,6 +109,21 @@ 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" + 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 From c15b099301fab75123872f3aadd9b57af080d521 Mon Sep 17 00:00:00 2001 From: Drake Stapleton Date: Mon, 5 Oct 2026 10:03:48 -0500 Subject: [PATCH 2/2] R15 preflight: mirror r15_energy_open/read exactly (5+5 labels, overflow_raw, power inputs), headroom check, seal blocked runs Label tables are read from tests/runtime/r15_measure.c so they cannot drift. Spec section 17 headroom (600 W bound) checked for R15_ENERGY_HEADROOM_SECONDS (default 300 s); per-window refusal stays in research/m15/spbm/sample.c. Blocked run directories get SHA256SUMS and are preserved. Co-Authored-By: Claude Opus 5.5 --- .crumb | 18 +++---- tests/.crumb | 8 +-- tests/r15_energy_preflight_test.sh | 82 +++++++++++++++--------------- tools/.crumb | 8 +-- tools/r15_machine_state.sh | 67 +++++++++++++++++------- tools/r15_qualify.sh | 2 + 6 files changed, 110 insertions(+), 75 deletions(-) diff --git a/.crumb b/.crumb index b3d6bfd97..61248e065 100644 --- a/.crumb +++ b/.crumb @@ -93,27 +93,27 @@ "purpose": "Directory src. Purpose not yet described by a human." }, { - "digest": "0dd887087aad1ba31bef855b4760172134c4a94a673dc940d7055ef88681d161", - "last_commit": "3822a0d", + "digest": "3d15ce0a4e66566f1d0e5ba22d84982aa0881dacf98732c2fa56b935406a8751", + "last_commit": "ec3659f", "name": "tests", "purpose": "Directory tests. Purpose not yet described by a human." }, { - "digest": "1a09a6c77f3da2f0c244cdbbc6d2e2ec3fd4908ade5b32a762db4e8b7899372f", - "last_commit": "a386d65", + "digest": "5844e06455eea3780afb1dd3b3351ace41e1648a721491c692783c057fd2479d", + "last_commit": "ec3659f", "name": "tools", "purpose": "Directory tools. Purpose not yet described by a human." } ], - "children_root": "sha256:7910d31ff39692b4dae41020e7ec3e6dd9c33041e9df1526cc0573f385808b13", - "compiled_at": "2026-10-05T14:55:30Z", + "children_root": "sha256:41dab9e8a3aa8db7675d36d6d394395b5bfeb9ff64e2212bc5a2b991134cf881", + "compiled_at": "2026-10-05T15:03:46Z", "compiler": "crumb-compile/1", - "digest": "sha256:6aac6d9faee310edf51bccf7c4298d5547f0a278e461c506c9b3c15e6fd008b0", + "digest": "sha256:712e4a8b0cdc4672bb315e5fbe799cbf24ca9eda6b0011cc2a706f7b43f8a5d4", "evidence_files": 2749, "evidence_root": "sha256:c6e60c5b2803cfddf6a1fed130c92066d285ddb68aeea0bc65d58668cf139dfc", "files": 4539, - "generated_at_commit": "a386d65ed193a02dccac444de3fd3224c96162b4", - "source_tree": "sha256:1e604db9c283e57175c287c8217b6d0de67eddaafeabdfb9e90897baf0c90f43" + "generated_at_commit": "ec3659fd8cda2fc4b16dbd4e729dae172ccdae6a", + "source_tree": "sha256:63721a48df45ecf4fe43294a06c2c5004307f25869633b0d2170af4ed96da90f" }, "owner_lane": "", "provenance": { diff --git a/tests/.crumb b/tests/.crumb index 9ce62981b..68ff3fed0 100644 --- a/tests/.crumb +++ b/tests/.crumb @@ -284,14 +284,14 @@ } ], "children_root": "sha256:215c9654ba0e4f579f3f1c7f09b3ecdacc7c922de1af68565150b55fedf9729a", - "compiled_at": "2026-10-05T14:55:30Z", + "compiled_at": "2026-10-05T15:03:46Z", "compiler": "crumb-compile/1", - "digest": "sha256:0dd887087aad1ba31bef855b4760172134c4a94a673dc940d7055ef88681d161", + "digest": "sha256:3d15ce0a4e66566f1d0e5ba22d84982aa0881dacf98732c2fa56b935406a8751", "evidence_files": 11, "evidence_root": "sha256:d5c1bceb4d57ef0eb42797c58801b5926509711614c3e38bb7105b6efa3942f2", "files": 798, - "generated_at_commit": "a386d65ed193a02dccac444de3fd3224c96162b4", - "source_tree": "sha256:89958e99dbb2820095288f778b832dedb3b398d9b1be0d9aafc3f3d0cee7fd99" + "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 index c9c044d31..a4d640acd 100644 --- a/tests/r15_energy_preflight_test.sh +++ b/tests/r15_energy_preflight_test.sh @@ -2,59 +2,61 @@ # 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) when the reader is absent or gives invalid samples, and pass (exit 0) -# when it gives a valid advancing counter. Fixtures are fake hwmon trees built -# in a temp dir; nothing touches the machine. The receipt writer must refuse a -# BLOCKED_INSTRUMENT run directory (exit 4) and write nothing. +# (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 +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 name label first-value-file-content - mkdir -p "$W/$1/hwmon0" "$W/$1/hwmon1" - echo acpitz > "$W/$1/hwmon0/name" - echo "$2" > "$W/$1/hwmon1/name" - echo "$3" > "$W/$1/hwmon1/energy1_label" +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 } -# reader absent: only unrelated hwmon devices -mkdir -p "$W/absent/hwmon0"; echo acpitz > "$W/absent/hwmon0/name" -check absent 5 "INSTRUMENT_UNAVAILABLE reason=absent" -mkdir -p "$W/empty" -check empty 5 "INSTRUMENT_UNAVAILABLE reason=absent" -# reader present, invalid samples -mk zero aien_spbm pkg; echo 0 > "$W/zero/hwmon1/energy1_input" -check zero 5 "reason=invalid" -mk unreadable aien_spbm pkg # energy1_input missing (kernel returned an error) -check unreadable 5 "reason=invalid" -mk garbage aien_spbm pkg; echo "-22" > "$W/garbage/hwmon1/energy1_input" -check garbage 5 "reason=invalid" -mk stuck aien_spbm pkg; echo 5000000 > "$W/stuck/hwmon1/energy1_input" -check stuck 5 "counter did not advance" -mk wronglabel aien_spbm gpm; echo 5000000 > "$W/wronglabel/hwmon1/energy1_input" -check wronglabel 5 "energy1_label is not pkg" -mk overflow aien_spbm pkg; echo 5000000 > "$W/overflow/hwmon1/energy1_input"; echo 1 > "$W/overflow/hwmon1/energy1_overflow_raw" -check overflow 5 "overflow indicator" -# another energy source must not be accepted in place of the reader -mk substitute nvml pkg; echo 5000000 > "$W/substitute/hwmon1/energy1_input" -check substitute 5 "reason=absent" -# valid reader: a counter that advances between samples -mk valid aien_spbm pkg; echo 100000 > "$W/valid/hwmon1/energy1_input"; echo 0 > "$W/valid/hwmon1/energy1_overflow_raw" -# a static file cannot advance; emulate with a FIFO-free trick: a shell function is not -# possible across processes, so use sleep-based writer for the valid case -( sleep 0.05; echo 100500 > "$W/valid/hwmon1/energy1_input"; sleep 0.05; echo 101000 > "$W/valid/hwmon1/energy1_input" ) & -export R15_ENERGY_PREFLIGHT_SLEEP=0.08 -check valid 0 "energy preflight ok" +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 -export R15_ENERGY_PREFLIGHT_SLEEP=0 -# receipt writer: a blocked run is refused with its own exit code, no output written +# 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 a15b6a84b..6e99b44b8 100644 --- a/tools/.crumb +++ b/tools/.crumb @@ -56,14 +56,14 @@ } ], "children_root": "sha256:3f9857a8b0946837f5556bcea0b4deaddd3a5aa3a60a31aa08ac917ac69d19f9", - "compiled_at": "2026-10-05T14:55:30Z", + "compiled_at": "2026-10-05T15:03:46Z", "compiler": "crumb-compile/1", - "digest": "sha256:1a09a6c77f3da2f0c244cdbbc6d2e2ec3fd4908ade5b32a762db4e8b7899372f", + "digest": "sha256:5844e06455eea3780afb1dd3b3351ace41e1648a721491c692783c057fd2479d", "evidence_files": 0, "evidence_root": "sha256:e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855", "files": 132, - "generated_at_commit": "a386d65ed193a02dccac444de3fd3224c96162b4", - "source_tree": "sha256:cb9af1c273557a94532ee5e79118aa447a8af94dfdf07c6ba69aa1e9d55c42bc" + "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 34320be7a..8e14c4042 100755 --- a/tools/r15_machine_state.sh +++ b/tools/r15_machine_state.sh @@ -51,9 +51,30 @@ if [ "${1:-}" = preflight-parse ]; then preflight_ghz "${2:?perf csv}"; exit $?; # (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 h="" d s v prev="" first="" n=0 ov + 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 @@ -61,29 +82,39 @@ energy_preflight() { 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 - if [ "$(cat "$h/energy1_label" 2>/dev/null)" != pkg ]; then - echo "INSTRUMENT_UNAVAILABLE reason=invalid: energy1_label is not pkg at $h" - return 5 - fi - ov=$(cat "$h/energy1_overflow_raw" 2>/dev/null) - if [ -n "$ov" ] && [ "$ov" != 0 ]; then - echo "INSTRUMENT_UNAVAILABLE reason=invalid: energy1 overflow indicator is $ov" - 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 - v=$(cat "$h/energy1_input" 2>/dev/null) - case "$v" in ''|*[!0-9]*) echo "INSTRUMENT_UNAVAILABLE reason=invalid: sample $s of energy1_input is not a number ('$v')"; return 5;; esac - if [ "$v" -le 0 ]; then echo "INSTRUMENT_UNAVAILABLE reason=invalid: sample $s reads $v uJ (a running package counter is above zero)"; return 5; fi - if [ -n "$prev" ] && [ "$v" -lt "$prev" ]; then echo "INSTRUMENT_UNAVAILABLE reason=invalid: counter went backwards ($prev -> $v uJ)"; return 5; fi - [ -z "$first" ] && first=$v - prev=$v; n=$((n + 1)) + 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: counter did not advance over $n samples ($first uJ)" + 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" + 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 diff --git a/tools/r15_qualify.sh b/tools/r15_qualify.sh index ead44d678..48b569dc4 100755 --- a/tools/r15_qualify.sh +++ b/tools/r15_qualify.sh @@ -119,6 +119,8 @@ if [ "$MODE" = silicon ]; 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"