interplane.Get Omega
Read in your language

aien-sovereign-core-mv

AienOS host runtime: inference scheduling, paged KV state, the aien CLI and Spark verification tooling. Its inference engine is in alpha testing (DGX Spark GB10); Omega is the implementation destination.

Brought by drakestapleton

branch main Branches Tags History

aien-sovereign-core-mv / scripts / bench / headstart-selftest.sh

359 lines · 27110 bytes Raw Blame History
1#!/usr/bin/env 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# Self-test for the Headstart M0 runner and summarizer (#532). Bash + jq only.
18# Runs the summarizer on synthetic data, then the full runner end to end on a
19# tiny throwaway Cargo workspace (never on the real repo), plus negative cases
20# that must fail: corrupt samples, missing UNIT_DATA, a build that does not
21# compile, a missing edit anchor and a build that dirties the source tree.
22#
23# bash scripts/bench/headstart-selftest.sh (exit 0 = all checks passed)
24set -uo pipefail
25
26HERE=$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)
27RUNNER="$HERE/headstart-aien.sh"
28SUM="$HERE/headstart-summarize.sh"
29TOOLCHAIN_FILE="$HERE/../../rust-toolchain.toml"
30T=$(mktemp -d "${TMPDIR:-/tmp}/headstart-selftest.XXXXXX")
31if [ "${HS_SELFTEST_KEEP:-0}" = 1 ]; then echo "keeping $T"; else trap 'rm -rf "$T"' EXIT; fi
32PASS=0; FAIL=0
33
34check() { # check <name> <command...>
35 local name=$1; shift
36 if "$@" >/dev/null 2>&1; then PASS=$((PASS + 1)); echo "ok $name"
37 else FAIL=$((FAIL + 1)); echo "FAIL $name"; fi
38}
39jqt() { jq -e "$@"; } # jq truthiness test: [jq args] <filter> <file>
40jqs() { jq -e -s "$@"; } # same, on a JSONL file slurped into an array
41
42# ------------------------------------------------ 1. summarizer, synthetic
43S="$T/samples.jsonl"
44mk() { # mk scenario arm wall rc final contaminated aa
45 jq -cn --arg s "$1" --arg a "$2" --argjson w "$3" --argjson rc "$4" --argjson f "$5" \
46 --argjson c "$6" --arg aa "$7" \
47 '{scenario: $s, arm: $a, kind: "cold", command: "cargo x", sample_id: ($s + "-" + $a + "-" + ($w|tostring)),
48 wall_s: $w, exit_status: $rc, ok: ($rc == 0), final: $f, contaminated: $c,
49 aa_pair: (if $aa == "-" then null else $aa end),
50 time_v: {available: true, max_rss_kb: 1000, cpu_percent: 500}}' >> "$S"
51}
52mk s1 A 10 0 true false -
53mk s1 A 12 0 true false -
54mk s1 A 11 0 true true -
55mk s1 A 5 101 true false -
56mk s1 A 99 0 false true -
57mk aa A 10 0 true false aa
58mk aa A 20 0 true false aa
59mk aa B 12 0 true false aa
60mk aa B 22 0 true false aa
61"$SUM" samples "$S" > "$T/summary.json"
62G='.groups[] | select(.scenario == "s1")'
63check "summary median excludes failed and superseded" jqt "$G | .wall_s.median == 11 and .wall_s.n == 3" "$T/summary.json"
64check "summary min/max/mad/stdev" jqt "$G | .wall_s.min == 10 and .wall_s.max == 12 and .wall_s.mad == 1 and .wall_s.stdev == 1" "$T/summary.json"
65check "summary counts failures with exit status" jqt "$G | .n_failed == 1 and .failures[0].exit_status == 101" "$T/summary.json"
66check "summary clean subset drops contaminated" jqt "$G | .wall_s_clean.n == 2 and .wall_s_clean.median == 11" "$T/summary.json"
67check "summary superseded count" jqt "$G | .n_superseded == 1" "$T/summary.json"
68check "A/A delta" jqt '.aa_noise[0] | .median_a_s == 15 and .median_b_s == 17 and .delta_s == 2 and .delta_pct == 13.333' "$T/summary.json"
69"$SUM" samples "$S" > "$T/summary2.json"
70check "summary is byte-for-byte reproducible" cmp "$T/summary.json" "$T/summary2.json"
71cp "$S" "$T/bad.jsonl"; echo '{"scenario": broken' >> "$T/bad.jsonl"
72check "NEGATIVE: corrupt samples line is rejected" bash -c "! '$SUM' samples '$T/bad.jsonl'"
73: > "$T/empty.jsonl"
74check "NEGATIVE: empty samples file is rejected" bash -c "! '$SUM' samples '$T/empty.jsonl'"
75
76# ------------------------------------------------ 2. timings + critical path
77cat > "$T/timing.html" <<'EOF'
78<html><script>
79DURATION = 6;
80const UNIT_DATA = [
81 {"i": 0, "name": "a", "version": "0.1.0", "mode": "todo", "target": "", "features": [], "start": 0.0, "duration": 4.0,
82 "unblocked_units": [], "unblocked_rmeta_units": [1], "sections": [["frontend", {"start": 0.0, "end": 1.0}], ["codegen", {"start": 1.0, "end": 4.0}]]},
83 {"i": 1, "name": "b", "version": "0.1.0", "mode": "todo", "target": "", "features": [], "start": 1.1, "duration": 3.0,
84 "unblocked_units": [2], "unblocked_rmeta_units": [], "sections": [["frontend", {"start": 0.0, "end": 2.0}], ["codegen", {"start": 2.0, "end": 3.0}]]},
85 {"i": 2, "name": "c", "version": "0.1.0", "mode": "todo", "target": " c \"bin\"", "features": [], "start": 4.2, "duration": 1.0,
86 "unblocked_units": [], "unblocked_rmeta_units": [], "sections": null}
87];
88const CONCURRENCY_DATA = [];
89</script></html>
90EOF
91"$SUM" timings "$T/timing.html" > "$T/units.json"
92check "timings extracts UNIT_DATA" jqt 'length == 3 and .[2].name == "c"' "$T/units.json"
93"$SUM" critical "$T/units.json" 2 > "$T/crit.json"
94check "critical chain a -> b -> c" jqt '[.observed.chain[].name] == ["a","b","c"]' "$T/crit.json"
95check "critical gates rmeta then full" jqt '[.observed.chain[1:][].gated_by.gate] == ["rmeta","full"] and .observed.chain[1].gate_time_s == 1 and .observed.chain[2].gate_time_s == 4.1' "$T/crit.json"
96check "estimate bounds" jqt '.estimate.floor_s == 4 and .estimate.rmeta_wait_on_chain_s == 1 and .estimate.upper_bound_saving_s == 1 and .estimate.scope == "cold cargo build --workspace only"' "$T/crit.json"
97# list-schedule replay: a(lib) -> b(lib, rmeta edge) -> c(bin, full edge)
98cat > "$T/graph.json" <<'EOF'
99[{"name": "a", "version": "0.1.0", "links": null, "build_script": false, "proc_macro": false, "deps": []},
100 {"name": "b", "version": "0.1.0", "links": null, "build_script": false, "proc_macro": false,
101 "deps": [{"name": "a", "version": "0.1.0", "kinds": ["normal"]}]},
102 {"name": "c", "version": "0.1.0", "links": null, "build_script": false, "proc_macro": false,
103 "deps": [{"name": "b", "version": "0.1.0", "kinds": ["normal"]}]}]
104EOF
105"$SUM" replay "$T/units.json" "$T/graph.json" 2 > "$T/replay2.json"
106check "replay 2 jobs: baseline 5, no rmeta wait 4, saving 1" jqt '.baseline_model_end_s == 5 and .no_rmeta_wait_model_end_s == 4 and .realistic_saving_s == 1 and .rmeta_edges == 1 and .deadlock == false' "$T/replay2.json"
107"$SUM" replay "$T/units.json" "$T/graph.json" 1 > "$T/replay1.json"
108check "replay 1 job: job limit removes the saving" jqt '.baseline_model_end_s == 8 and .no_rmeta_wait_model_end_s == 8 and .realistic_saving_s == 0' "$T/replay1.json"
109jq '(.[] | select(.name == "a") | .proc_macro) = true' "$T/graph.json" > "$T/graph-pm.json"
110"$SUM" replay "$T/units.json" "$T/graph-pm.json" 2 > "$T/replay-pm.json"
111check "replay: proc-macro dependency is a full edge (no saving)" jqt '.rmeta_edges == 0 and .realistic_saving_s == 0' "$T/replay-pm.json"
112check "NEGATIVE: replay without a graph is rejected" bash -c "! '$SUM' replay '$T/units.json' '$T/missing.json' 2"
113printf '<html>no data</html>\n' > "$T/nodata.html"
114check "NEGATIVE: report without UNIT_DATA is rejected" bash -c "! '$SUM' timings '$T/nodata.html'"
115
116# ------------------------------------------------ 3. runner end to end on a fixture
117mkfixture() { # mkfixture <dir> [broken_runtime] [dirty_buildrs]
118 local d=$1 broken=${2:-no} dirty=${3:-no}
119 mkdir -p "$d/crates"/{hsbase,spark-harness,spark-supervisor,aien-runtime}/src
120 printf '[workspace]\nresolver = "2"\nmembers = ["crates/hsbase", "crates/spark-harness", "crates/spark-supervisor", "crates/aien-runtime"]\n' > "$d/Cargo.toml"
121 [ -f "$TOOLCHAIN_FILE" ] && cp "$TOOLCHAIN_FILE" "$d/rust-toolchain.toml"
122 pkg() { printf '[package]\nname = "%s"\nversion = "0.1.0"\nedition = "2021"\n%s\n' "$1" "$2"; }
123 pkg hsbase "$([ "$dirty" = yes ] && echo 'build = "build.rs"')" > "$d/crates/hsbase/Cargo.toml"
124 printf 'pub fn core_value() -> u64 {\n 7\n}\n' > "$d/crates/hsbase/src/lib.rs"
125 if [ "$dirty" = yes ]; then
126 printf 'fn main() {\n let p = concat!(env!("CARGO_MANIFEST_DIR"), "/src/lib.rs");\n let mut s = std::fs::read_to_string(p).unwrap();\n s.push_str("\\n// dirtied by build script\\n");\n std::fs::write(p, s).unwrap();\n}\n' > "$d/crates/hsbase/build.rs"
127 fi
128 pkg spark-harness '[dependencies]
129hsbase = { path = "../hsbase" }' > "$d/crates/spark-harness/Cargo.toml"
130 printf 'pub fn harness() -> u64 {\n hsbase::core_value() + 1\n}\n' > "$d/crates/spark-harness/src/lib.rs"
131 pkg spark-supervisor '[dependencies]
132hsbase = { path = "../hsbase" }' > "$d/crates/spark-supervisor/Cargo.toml"
133 printf 'fn main() {\n println!("{}", hsbase::core_value());\n}\n' > "$d/crates/spark-supervisor/src/main.rs"
134 pkg aien-runtime '[dependencies]
135spark-harness = { path = "../spark-harness" }' > "$d/crates/aien-runtime/Cargo.toml"
136 if [ "$broken" = yes ]; then
137 printf 'pub fn runtime() -> u64 {\n spark_harness::harness() + "not a number"\n}\n' > "$d/crates/aien-runtime/src/lib.rs"
138 else
139 printf 'pub fn runtime() -> u64 {\n spark_harness::harness() * 2\n}\n' > "$d/crates/aien-runtime/src/lib.rs"
140 fi
141 (cd "$d" && git init -q && cargo generate-lockfile -q --offline && git add -A \
142 && git -c user.name=selftest -c user.email=selftest@invalid commit -q -m fixture)
143}
144
145ANCHOR_DEFAULT='pub fn core_value() -> u64 {'
146runner() { # runner <fixture> <tag> [stages...]
147 local fx=$1 tag=$2; shift 2
148 HS_REPO="$fx" HS_COMMIT=HEAD HS_WORK="$T/work-$tag" HS_OUT="$T/out-$tag" HS_DATE=selftest \
149 HS_SAMPLES=1 HS_LOAD_MAX="${T_LOAD_MAX:-100000}" HS_WAIT_CAP_S=0 HS_MAX_RERUNS=0 HS_MON_INTERVAL_S=1 \
150 HS_QUIETLOCK_BIN="${T_QL_BIN:-quietlock}" HS_QUIET_WAIT_CAP_S="${T_QL_CAP:-3600}" \
151 HS_INCR_STEPS=2 HS_INCR_SERIES=1 HS_BODY_FILE=crates/hsbase/src/lib.rs \
152 HS_BODY_ANCHOR="${HS_TEST_ANCHOR:-$ANCHOR_DEFAULT}" HS_IFACE_FILE=crates/hsbase/src/lib.rs \
153 HS_EDIT_REASON=selftest bash "$RUNNER" "$@" >> "$T/runner-$tag.log" 2>&1
154}
155
156FX="$T/fixture"; mkfixture "$FX"
157runner "$FX" good setup timings cold incr cleanup
158RC=$?
159O="$T/out-good"
160check "runner full matrix exits 0" test "$RC" -eq 0
161check "runner wrote 18 samples, all ok" jqs 'length == 18 and all(.[]; .ok)' "$O/samples.jsonl"
162check "runner manifest has commit, rustc and scenarios" jqt '(.source.commit | length) == 40 and (.toolchain.rustc_vV | test("rustc ")) and (.scenarios | length) >= 11 and .build.registry_cache.state == "warm"' "$O/manifest.json"
163check "runner records quiet-flag and load waits per sample" jqs 'all(.[]; (.quiet_flag_wait_s | type) == "number" and (.load_wait_s | type) == "number" and (.loadavg_before | length) == 3)' "$O/samples.jsonl"
164check "runner sample ids are unique" jqs 'map(.sample_id) | length == (unique | length)' "$O/samples.jsonl"
165check "runner keeps one log tail per sample" bash -c "[ \$(ls '$O/logs' | wc -l) -eq 18 ]"
166check "runner summary reproducible from samples" bash -c "'$SUM' samples '$O/samples.jsonl' | cmp - '$O/summary.json'"
167check "runner A/A arms present" jqt '.aa_noise[0].n_a == 1 and .aa_noise[0].n_b == 1' "$O/summary.json"
168check "runner critical path extracted" jqt '.observed.chain_length >= 2' "$O/timings/critical-path.json"
169check "body edits recompiled the edited crate" grep -q 'Checking hsbase' "$O/logs/incr_body_check-A-r1-s1-a1.tail.log"
170check "iface edits recorded the edit" jqs 'map(select(.scenario == "incr_iface_check")) | length == 2 and all(.[]; .edit | test("pub fn headstart_m0_probe_"))' "$O/samples.jsonl"
171check "caller fixture checkout untouched" git -C "$FX" diff --quiet
172check "benchmark worktree removed" bash -c "[ \$(git -C '$FX' worktree list | wc -l) -eq 1 ] && [ ! -e '$T/work-good' ]"
173
174HS_TEST_ANCHOR='no such anchor line' runner "$FX" anchor setup incr
175RC=$?
176check "NEGATIVE: missing edit anchor aborts" test "$RC" -ne 0
177check "NEGATIVE: abort message names the anchor" grep -q 'body anchor must match exactly one line' "$T/runner-anchor.log"
178check "NEGATIVE: aborted run left worktree clean" git -C "$T/work-anchor/src" diff --quiet
179runner "$FX" anchor cleanup
180
181FXB="$T/fixture-broken"; mkfixture "$FXB" yes
182runner "$FXB" broken setup cold cleanup
183RC=$?
184check "broken build: runner still exits 0 (failure recorded, not hidden)" test "$RC" -eq 0
185check "NEGATIVE: failed build recorded with exit status" jqs 'map(select(.scenario == "cold_build_aien_runtime")) | length == 1 and .[0].ok == false and .[0].exit_status != 0' "$T/out-broken/samples.jsonl"
186check "NEGATIVE: failed build never counted in median" jqt '.groups[] | select(.scenario == "cold_build_aien_runtime") | .n_failed == 1 and .wall_s.n == 0 and .wall_s.median == null' "$T/out-broken/summary.json"
187check "broken build: unaffected crate still measured" jqt '.groups[] | select(.scenario == "cold_build_spark_harness") | .wall_s.n == 1' "$T/out-broken/summary.json"
188
189FXD="$T/fixture-dirty"; mkfixture "$FXD" no yes
190runner "$FXD" dirty setup timings
191RC=$?
192check "NEGATIVE: build that dirties the worktree aborts the run" test "$RC" -ne 0
193check "NEGATIVE: abort message names the dirty tree" grep -q 'not clean' "$T/runner-dirty.log"
194git -C "$T/work-dirty/src" checkout -q -- . 2>/dev/null
195runner "$FXD" dirty cleanup
196
197# Load wait that hits its cap: HS_LOAD_MAX=0 can never be met, cap is 0 s.
198T_LOAD_MAX=0 runner "$FX" capped setup timings cleanup
199RC=$?
200check "capped load wait: run completes" test "$RC" -eq 0
201check "NEGATIVE: capped load wait marks the sample contaminated" jqs 'length == 1 and .[0].load_wait_capped == true and .[0].contaminated == true and (.[0].contamination_reasons | index("load_wait_capped"))' "$T/out-capped/samples.jsonl"
202
203# quietlock missing: no check, recorded truthfully.
204T_QL_BIN="$T/no-such-quietlock" runner "$FX" noql setup timings cleanup
205check "quietlock missing: manifest says not available, not respected" jqt '.protocol.quiet_flag.available == false and .protocol.quiet_flag.respected == false' "$T/out-noql/manifest.json"
206check "quietlock missing: sample says not checked" jqs '.[0].quiet_flag_checked == false and .[0].quiet_flag_wait_s == 0' "$T/out-noql/samples.jsonl"
207
208# Stale quiet flag: a fake quietlock that always reports "held" (exit 75).
209printf '#!/usr/bin/env bash\necho "fake quietlock: held" >&2\nexit 75\n' > "$T/fake-quietlock"; chmod +x "$T/fake-quietlock"
210T_QL_BIN="$T/fake-quietlock" T_QL_CAP=2 timeout 300 bash -c "$(declare -f runner); T='$T' RUNNER='$RUNNER' ANCHOR_DEFAULT='$ANCHOR_DEFAULT' runner '$FX' stale setup timings cleanup"
211RC=$?
212check "NEGATIVE: stale quiet flag does not hang the run (cap)" test "$RC" -eq 0
213check "NEGATIVE: capped quiet-flag wait is logged" grep -q 'quiet flag still held after' "$T/runner-stale.log"
214check "NEGATIVE: capped quiet-flag wait marks the sample contaminated" jqs '.[0].quiet_flag_checked == true and .[0].quiet_flag_wait_capped == true and .[0].quiet_flag_wait_s >= 2 and (.[0].contamination_reasons | index("quiet_flag_wait_capped"))' "$T/out-stale/samples.jsonl"
215check "quietlock present: manifest says available and respected" jqt '.protocol.quiet_flag.available == true and .protocol.quiet_flag.respected == true and .protocol.quiet_flag.wait_cap_s == 2' "$T/out-stale/manifest.json"
216
217# A foreign "cargo" process (a renamed sleep, outside the runner tree) must be
218# seen at start, during (1 s ticks) and in the end-of-sample snapshot.
219cp "$(command -v sleep)" "$T/cargo"
220"$T/cargo" 60 & FAKE=$!
221runner "$FX" foreign setup timings cleanup
222kill "$FAKE" 2>/dev/null; wait "$FAKE" 2>/dev/null
223check "NEGATIVE: foreign compiler process is detected at start and end" jqs --arg p "$FAKE:cargo:-" '.[0].contaminated == true and (.[0].foreign.at_start | index($p)) and (.[0].foreign.at_end | index($p)) and (.[0].contamination_reasons | index("foreign_at_end"))' "$T/out-foreign/samples.jsonl"
224check "timings stage writes the replay estimate" jqt '.deadlock == false and .units_without_package == 0 and .label == "ESTIMATE, not a measurement"' "$T/out-foreign/timings/replay.json"
225
226# ------------------------------------------------ 4. M5 (#536) OFF/ON summary, synthetic
227SAB="$T/ab.jsonl"
228mkab() { # mkab scenario arm wall rc rss user [contaminated]
229 jq -cn --arg s "$1" --arg a "$2" --argjson w "$3" --argjson rc "$4" --argjson rss "$5" --argjson u "$6" --argjson c "${7:-false}" \
230 '{scenario: $s, arm: $a, kind: "ab", command: "cargo x", sample_id: ($s + "-" + $a + "-" + ($w|tostring)),
231 wall_s: $w, exit_status: $rc, ok: ($rc == 0), final: true, contaminated: $c, aa_pair: null,
232 time_v: {available: true, max_rss_kb: $rss, cpu_percent: 100, user_s: $u, sys_s: 1}}' >> "$SAB"
233}
234mkab w OFF 10 0 1000 9; mkab w OFF 12 0 1000 9; mkab w ON 8 0 1100 10; mkab w ON 9 0 1100 10; mkab w ON 1 101 1 1
235mkab w OFF 100 0 9000 90 true; mkab w ON 0.5 0 1 1 true
236mkab c OFF 10 0 1000 9 true; mkab c ON 9 0 1000 9 true; mkab f OFF 10 0 1000 9; mkab f ON 1 101 1 1
237"$SUM" samples "$SAB" > "$T/ab-summary.json"
238AB='.ab[] | select(.scenario == "w")'
239check "ab: medians, delta and saving" jqt "$AB | .wall_s_off.median == 11 and .wall_s_on.median == 8.5 and .delta_s == -2.5 and .saving_pct == 22.727" "$T/ab-summary.json"
240check "ab: failed ON sample counted as failure, not time" jqt "$AB | .failed_on == 1 and .n_on == 2 and .failures[0].arm == \"ON\" and .failures[0].exit_status == 101" "$T/ab-summary.json"
241check "NEGATIVE: contaminated finals excluded from times, RSS and CPU" jqt "$AB | .contaminated_excluded_off == 1 and .contaminated_excluded_on == 1 and .n_off == 2 and .max_rss_kb_median_off == 1000 and .status == \"measured\"" "$T/ab-summary.json"
242check "NEGATIVE: only contaminated samples give NOT_MEASURED, no number" jqt '.ab[] | select(.scenario == "c") | .status == "NOT_MEASURED" and .n_off == 0 and .n_on == 0 and .saving_pct == null' "$T/ab-summary.json"
243check "NEGATIVE: ON that only fails gives FAILED, no number" jqt '.ab[] | select(.scenario == "f") | .status == "FAILED" and .failed_on == 1 and .saving_pct == null' "$T/ab-summary.json"
244check "ab: max RSS and CPU deltas" jqt "$AB | .max_rss_delta_pct == 10 and .cpu_s_median_off == 10 and .cpu_s_median_on == 11 and .cpu_s_delta_pct == 10" "$T/ab-summary.json"
245check "ab: section absent when no OFF/ON arms" jqt 'has("ab") | not' "$T/summary.json"
246
247# ------------------------------------------------ 5. M5 A/B stages end to end on the fixture
248ABSTAGES=(setup ab abincr abverify cleanup)
249HS_ARM_ENV_OFF="HS_FAKE_ARM=off" HS_ARM_ENV_ON="HS_FAKE_ARM=on" HS_AB_JOBS=2 HS_AB_JOBS_SAMPLES=1 \
250HS_VERIFY_TEST_PKGS=hsbase HS_VERIFY_ENV_ON="HS_FAKE_VERIFY=1" runner "$FX" ab "${ABSTAGES[@]}"
251RC=$?
252O="$T/out-ab"
253check "ab runner exits 0" test "$RC" -eq 0
254check "ab runner wrote 35 samples, all ok" jqs 'length == 35 and all(.[]; .ok)' "$O/samples.jsonl"
255check "ab arm environments reach the command" jqs 'all(.[] | select(.arm == "ON"); .env_words | test("HS_FAKE_ARM=on")) and all(.[] | select(.arm != "ON"); .env_words | test("HS_FAKE_ARM=off"))' "$O/samples.jsonl"
256check "ab arms use separate target dirs" jqs '[.[] | select(.kind == "ab")] | length == 10 and all(.[]; . as $x | $x.cargo_target_dir | endswith("-" + $x.arm))' "$O/samples.jsonl"
257check "ab A/A pair runs with the OFF environment" jqt '.aa_noise[] | select(.scenario == "ab_aa_check_ws") | .n_a == 1 and .n_b == 1' "$O/summary.json"
258# Coverage counts contaminated finals too: on a busy host a fixture sample
259# can be contaminated (HS_MAX_RERUNS=0 here), which excludes it from times.
260check "ab summary covers cold, jobs and incremental scenarios" jqt '[.ab[] | select(.n_off + .contaminated_excluded_off >= 1 and .n_on + .contaminated_excluded_on >= 1) | .scenario] as $s | (["ab_cold_check_ws","ab_cold_build_ws","ab_cold_test_norun_ws","ab_cold_build_ws_j2","abincr_body_check","abincr_iface_check"] - $s) == []' "$O/summary.json"
261check "ab incremental: both arms check every edit" jqs '[.[] | select(.scenario == "abincr_body_check")] | (map(select(.arm == "OFF")) | length) == 2 and (map(select(.arm == "ON")) | length) == 2' "$O/samples.jsonl"
262check "ab verify: verdict match with test results" jqt '.verdict == "match" and .diagnostics_equal and .test_results_equal and (.arms.ON.test_results | length) >= 1 and .verify_build.exit == 0' "$O/verify.json"
263check "ab verify extra env added on top of the ON arm" jqs 'map(select(.scenario == "verify_build_extra")) | length == 1 and (.[0].env_words | test("HS_FAKE_ARM=on") and test("HS_FAKE_VERIFY=1"))' "$O/samples.jsonl"
264check "ab manifest records binaries digests and arm envs" jqt '(.toolchain.binaries.sha256 | length) >= 2 and .toolchain.binaries.arm_env.ON == "HS_FAKE_ARM=on"' "$O/manifest.json"
265check "ab summary reproducible from samples" bash -c "'$SUM' samples '$O/samples.jsonl' | cmp - '$O/summary.json'"
266check "ab caller fixture checkout untouched" git -C "$FX" diff --quiet
267
268HS_ARM_ENV_OFF="HS_FAKE_ARM=off" runner "$FX" noon setup ab
269RC=$?
270check "NEGATIVE: ab stage without HS_ARM_ENV_ON aborts" test "$RC" -ne 0
271check "NEGATIVE: abort message names the arm variables" grep -q 'HS_ARM_ENV_OFF and HS_ARM_ENV_ON' "$T/runner-noon.log"
272runner "$FX" noon cleanup
273HS_ARM_ENV_OFF="HS_FAKE_ARM=x" HS_ARM_ENV_ON="HS_FAKE_ARM=x" runner "$FX" same setup abverify
274RC=$?
275check "NEGATIVE: identical arm environments abort" test "$RC" -ne 0
276runner "$FX" same cleanup
277
278HS_ARM_ENV_OFF="HS_FAKE_ARM=off" HS_ARM_ENV_ON="RUSTFLAGS=-Wmissing-docs" HS_VERIFY_TEST_PKGS=hsbase \
279 runner "$FX" diag setup abverify cleanup
280check "NEGATIVE: changed diagnostics give verdict MISMATCH" jqt '.verdict == "MISMATCH" and (.diagnostics_equal | not) and .check_exit_equal and .arms.ON.diagnostics > .arms.OFF.diagnostics' "$T/out-diag/verify.json"
281
282HS_ARM_ENV_OFF="HS_FAKE_ARM=off" HS_ARM_ENV_ON="RUSTFLAGS=-Zheadstart-selftest-bogus" HS_VERIFY_TEST_PKGS=hsbase \
283 runner "$FX" brk setup ab abverify cleanup
284RC=$?
285check "broken ON arm: runner still exits 0 (failure recorded)" test "$RC" -eq 0
286check "NEGATIVE: failing ON arm recorded as failures, never as time" jqt '.ab[] | select(.scenario == "ab_cold_build_ws") | .failed_on == 1 and .n_on == 0 and .wall_s_on.median == null and .saving_pct == null' "$T/out-brk/summary.json"
287check "NEGATIVE: wrong ON exit code gives verdict MISMATCH" jqt '.verdict == "MISMATCH" and (.check_exit_equal | not) and .arms.ON.check_exit != 0' "$T/out-brk/verify.json"
288
289# ------------------------------------------------ 6. per-sample quiet-flag hold, ICE reports, failure tails
290# The real quietlock on a private state dir (QUIETLOCK_DIR), never the host's
291# flag. The fake cargo records whether our hold is on the flag while the timed
292# command runs, and (with HS_FAKE_ICE=1) fails a check the way the SVH bug
293# does: an ICE report in the cwd, the error far above 100 warning lines.
294QL="$T/ql"; mkdir -p "$QL"
295cat > "$T/fake-cargo" <<'EOF'
296#!/usr/bin/env bash
297if [ -n "${HS_FAKE_FLAG_LOG:-}" ]; then
298 if grep -q hs-selftest "$QUIETLOCK_DIR/.spark-quiet" 2>/dev/null; then echo HELD; else echo FREE; fi >> "$HS_FAKE_FLAG_LOG"
299fi
300if [ "${HS_FAKE_ICE:-}" = 1 ] && [[ " $* " == *" check "* ]]; then
301 printf "thread 'rustc' panicked at compiler/selftest.rs:1:1:\nassertion failed: selftest svh\nstack backtrace:\n 0: selftest-backtrace-frame\n" > "rustc-ice-selftest-$$.txt"
302 echo 'error: could not compile `hsbase` (lib)'
303 for i in $(seq 100); do echo "warning: selftest noise $i"; done
304 exit 101
305fi
306exec cargo "$@"
307EOF
308chmod +x "$T/fake-cargo"
309QL_BIN=$(command -v quietlock || true)
310if [ -n "$QL_BIN" ]; then
311 export QUIETLOCK_DIR="$QL"
312 hrunner() {
313 HS_HOLD=1 HS_HOLD_OWNER=hs-selftest HS_HOLD_MINUTES=1 HS_HOLD_REASON=selftest \
314 HS_CARGO="$T/fake-cargo" HS_FAKE_FLAG_LOG="$T/flag.log" runner "$@"
315 }
316 hrunner "$FX" hold setup
317 # A foreign hold taken between stages: the first sample must queue behind it.
318 "$QL_BIN" hold --owner selftest-other --minutes 1 --reason selftest -- sleep 5 >/dev/null 2>&1 &
319 OTHER=$!; sleep 1
320 hrunner "$FX" hold cold
321 RC=$?; wait "$OTHER" 2>/dev/null
322 O="$T/out-hold"; N=$(jq -s length "$O/samples.jsonl")
323 check "hold: run exits 0 with samples" test "$RC" -eq 0 -a "$N" -ge 2
324 check "hold: every sample held, one distinct hold id each" jqs 'all(.[]; .quiet_hold.held and .quiet_hold.requested) and ([.[].quiet_hold.id] | unique | length) == length and all(.[]; .contamination_reasons | index("quiet_hold_not_taken") | not)' "$O/samples.jsonl"
325 check "hold: our hold is on the flag during every timed command" test "$(grep -c HELD "$T/flag.log")" -eq "$N"
326 check "hold: setup commands outside samples run unheld" grep -q FREE "$T/flag.log"
327 check "hold: every hold acquired and released (history)" test "$(grep -c 'hold acquired: hs-selftest' "$QL/.spark-quiet.history")" -eq "$N" -a "$(grep -c 'released' "$QL/.spark-quiet.history")" -ge "$((N + 1))"
328 check "hold: flag clear after the run" "$QL_BIN" check
329 check "NEGATIVE: foreign hold is waited out before our hold" jqs '.[0].quiet_flag_wait_s >= 2 and .[0].quiet_hold.held' "$O/samples.jsonl"
330 check "hold: manifest lists the held samples" jqt --argjson n "$N" '.protocol.quiet_hold.enabled and .protocol.quiet_hold.owner == "hs-selftest" and (.protocol.quiet_hold.samples_held | length) == $n and .protocol.quiet_hold.samples_unheld == []' "$O/manifest.json"
331 runner "$FX" hold cleanup
332
333 # A quietlock whose check says clear but whose hold is always refused.
334 printf '#!/usr/bin/env bash\n[ "$1" = check ] && exit 0\necho "quietlock: QUIETLOCK_REFUSED fake" >&2\nexit 75\n' > "$T/refusing-quietlock"
335 chmod +x "$T/refusing-quietlock"
336 T_QL_BIN="$T/refusing-quietlock" T_QL_CAP=2 HS_HOLD=1 runner "$FX" refused setup cold cleanup
337 RC=$?
338 check "NEGATIVE: refused hold does not hang the run (cap)" test "$RC" -eq 0
339 check "NEGATIVE: refused hold marks the sample contaminated, unheld" jqs '.[0].quiet_hold.held == false and .[0].quiet_hold.refused >= 1 and .[0].contaminated and (.[0].contamination_reasons | index("quiet_hold_not_taken"))' "$T/out-refused/samples.jsonl"
340 unset QUIETLOCK_DIR
341else
342 check "quietlock installed for the hold tests" false
343fi
344
345HS_CARGO="$T/fake-cargo" HS_ARM_ENV_OFF="HS_FAKE_ARM=off" HS_ARM_ENV_ON="HS_FAKE_ICE=1" runner "$FX" ice setup ab
346RC=$?
347ICE='.[] | select(.scenario == "ab_cold_check_ws" and .arm == "ON")'
348check "ICE ON arm: runner exits 0, failure recorded" test "$RC" -eq 0
349check "NEGATIVE: ICE sample has exit 101 and one ICE report" jqs "[$ICE] | length == 1 and .[0].exit_status == 101 and .[0].ice_reports == 1" "$T/out-ice/samples.jsonl"
350check "OFF samples report no ICE" jqs 'all(.[] | select(.arm == "OFF"); .ice_reports == 0)' "$T/out-ice/samples.jsonl"
351TAILF="$T/out-ice/$(jq -rs "[$ICE][0].log_tail" "$T/out-ice/samples.jsonl")"
352check "failure tail starts with the error lines" bash -c "head -1 '$TAILF' | grep -q '^== error lines' && sed -n 2p '$TAILF' | grep -q 'could not compile'"
353check "failure tail carries the ICE panic head" grep -q 'assertion failed: selftest svh' "$TAILF"
354check "NEGATIVE: ICE backtrace is not copied" bash -c "! grep -q selftest-backtrace-frame '$TAILF'"
355check "ICE report removed from the worktree root" bash -c "! ls '$T/work-ice/src'/rustc-ice-* 2>/dev/null"
356runner "$FX" ice cleanup
357
358echo "selftest: $PASS passed, $FAIL failed"
359[ "$FAIL" -eq 0 ]