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 / docs / campaigns / smollm2-qualification / run-smol2q.sh

329 lines · 24400 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# SMOLLM2-Q qualification harness (the CAND-4 harness, copied; ACCEPTANCE-SMOLLM2-Q.md section 0 lists every change) (ACCEPTANCE-SMOLLM2-Q.md). Shell + jq only. Never retries a
18# launch; every launch, failed or not, leaves an attempt record named by its sha256.
19#
20# run-smol2q.sh check INPUTS recompute every pinned digest (exit 3 on a mismatch)
21# run-smol2q.sh hygiene INPUTS OUT section 3 (hygiene.sh)
22# run-smol2q.sh q1 INPUTS RUN_BASE OUT section 4: 3 rounds x (T1, T2, T3), GPU, one quietlock hold per round
23# run-smol2q.sh q2 INPUTS RUN_BASE OUT section 5: F0 on the GPU (one hold), then the cases on cpu_fault
24# run-smol2q.sh q2w INPUTS RUN_BASE OUT section 6.1: coverage cases 2 and 5 (run-windows.sh on cpu_fault, after q2)
25# run-smol2q.sh mem INPUTS RUN_BASE OUT section 8: memory observations (SmolLM2 + Llama control daemon starts, GPU, one hold)
26#
27# INPUTS = ACCEPTANCE-SMOLLM2-Q.md (kind QUALIFICATION, refused while UNFROZEN) or a TRIAL inputs
28# file (kind TRIAL: OUT must contain TRIAL and stay outside the evidence dir and the repo;
29# SMOL2Q_TRIAL_TASKS / SMOL2Q_TRIAL_REPS / SMOL2Q_TRIAL_CASES may narrow a TRIAL run only).
30# OUT layout: q1/ (NEXT-PHASE-1 receipts + replies), q2/ (NEXT-PHASE-2 receipt), attempts/,
31# scores/, hygiene/ ; every file there is named by the sha256 of its content.
32set -u
33HERE=$(cd "$(dirname "$0")" && pwd)
34REPO=$(cd "$HERE/../../.." && pwd)
35NP1=$REPO/docs/campaigns/next-phase-1 NP2=$REPO/docs/campaigns/next-phase-2 SCORE=$REPO/docs/campaigns/scoring/score-rows.sh
36. "$HERE/inputs.sh"
37CMD=${1:?check|hygiene|q1|q2}; INPUTS=${2:?INPUTS}
38load_inputs "$INPUTS" || exit 3
39KIND=$(inp kind)
40SC=$(inp sc_commit) OC=$(inp omega_commit) MD=$(inp model_dir)
41A1RULE=$(inp q1_a1_record_mark)
42whisper() { crumb whisper laneSmol2 "$REPO" - "$1" >/dev/null 2>&1 || echo "whisper failed: $1" >&2; }
43refuse_unless_digests() { verify_digests || { echo "REFUSED: a pinned digest differs (no launch started)" >&2; exit 3; }; }
44
45# content-addressed write: name_by_sha DIR PREFIX < json -> prints the file name
46name_by_sha() {
47 local t; t=$(mktemp "$1/.tmp.XXXXXX"); cat >"$t"
48 local h; h=$(sha256sum "$t" | cut -d' ' -f1); mv "$t" "$1/$2$h.json"; echo "$2$h.json"
49}
50# launch env: nothing from the caller's environment but these (ACCEPTANCE-SMOLLM2-Q section 2)
51clean_env() { env -i PATH="$PATH" HOME="$HOME" USER="${USER:-}" LANG="${LANG:-C.UTF-8}" "$@"; }
52gpu_env() {
53 clean_env AIEN_BIN="$(inp aien_cli_path)" AIEN_MODEL_PATH="$MD/model.safetensors" AIEN_TOKENIZER_PATH="$MD/tokenizer.json" \
54 AIEN_COMPOSE_MAX_TOKENS="$(inp max_tokens)" AIEN_REQUIRE_BLACKWELL=1 "$@"
55}
56# hold MINUTES REASON CMD...: wait for the quiet flag, then run CMD inside our quietlock hold
57# with start and release whispers. A hold that never started (the flag was taken first) is
58# waited out and taken again; CMD itself is never run twice (it leaves a started marker).
59hold() {
60 local min=$1 why=$2; shift 2
61 local mark; mark=$(mktemp -u "${TMPDIR:-/tmp}/smol2q-started.XXXXXX")
62 while :; do
63 wait_quiet "$why"
64 whisper "laneSmol2 GPU start: $why"
65 quietlock hold --owner laneSmol2 --minutes "$min" --reason "$why" -- bash -c 'touch "$0"; exec "$@"' "$mark" "$@"
66 local rc=$?
67 whisper "laneSmol2 GPU release: $why rc=$rc"
68 [ -e "$mark" ] && { rm -f "$mark"; return $rc; }
69 echo "quietlock: hold not taken (rc $rc), waiting" >&2; sleep 30
70 done
71}
72
73# a1_read RECEIPT MARK lives in a1-read.sh (shared with selftest.sh).
74. "$HERE/a1-read.sh"
75
76case $CMD in
77check)
78 verify_digests; rc=$?
79 jq -c '.[] | {name, ok, got: .got[0:16]}' <<<"$DIGESTS"
80 [ $rc = 0 ] && echo "all pinned digests equal the frozen values" || { echo "DIGEST CHECK FAILED"; exit 3; }
81 ;;
82
83hygiene)
84 OUT=${3:?OUT}; out_ok "$OUT" || exit 3
85 bash "$HERE/hygiene.sh" "$INPUTS" "$OUT/hygiene"
86 ;;
87
88q1)
89 BASE=${3:?RUN_BASE} OUT=${4:?OUT}; out_ok "$OUT" || exit 3
90 case $A1RULE in strict|record-mark) ;; *) echo "REFUSED: q1_a1_record_mark must be strict or record-mark (is: $A1RULE)" >&2; exit 3;; esac
91 mkdir -p "$BASE" "$OUT/q1" "$OUT/q1-a1" "$OUT/attempts" "$OUT/scores"; BASE=$(cd "$BASE" && pwd); OUT=$(cd "$OUT" && pwd)
92 [ -e "$OUT/q1-results.jsonl" ] && { echo "REFUSED: Q1 already ran into $OUT (never repeated)" >&2; exit 3; }
93 TASKS="T1 T2 T3" REPS=3
94 if [ "$KIND" = TRIAL ]; then TASKS=${SMOL2Q_TRIAL_TASKS:-$TASKS} REPS=${SMOL2Q_TRIAL_REPS:-$REPS}; fi
95 for r in $(seq 1 "$REPS"); do
96 refuse_unless_digests
97 hold 20 "SMOLLM2-Q Q1 round $r ($KIND)" bash "$0" _q1_round "$INPUTS" "$BASE" "$OUT" "$r" "$TASKS"
98 # a declared launch of this round that left no result line did not run: FAIL, recorded, not retried
99 touch "$OUT/q1-results.jsonl"
100 for id in $TASKS; do
101 jq -e --arg id "$id" --argjson r "$r" 'select(.row == $id and .rep == $r)' "$OUT/q1-results.jsonl" >/dev/null && continue
102 a=$(jq -n --arg k "$KIND" --arg sc "$SC" --arg id "$id" --argjson r "$r" \
103 '{gate:"SMOL2Q_Q1_ATTEMPT", kind:$k, sc_commit:$sc, row:$id, rep:$r, env:"GPU", verdict:"FAIL",
104 outcome:"launch did not run (the round ended before this task; see the hold output)"}' | name_by_sha "$OUT/attempts" q1-)
105 jq -nc --arg id "$id" --argjson r "$r" --arg a "$a" '{row:$id, rep:$r, verdict:"FAIL", outcome:("launch did not run; attempt " + $a), env:"GPU"}' >>"$OUT/q1-results.jsonl"
106 echo "Q1 $id rep $r: FAIL (launch did not run; attempts/$a)"
107 done
108 done
109 # Section 4.1 negative controls, run as campaign rows on copies of the first real receipt the
110 # reading accepted: a damaged mark must be rejected, and a receipt with one more failing row
111 # must be rejected. A control row is PASS iff the reading says FAIL. Nothing real is edited.
112 if [ "$A1RULE" = record-mark ]; then
113 base=$(for f in "$OUT"/q1-a1/a1-*.json; do [ -e "$f" ] || continue; [ "$(jq -r .verdict "$f")" = PASS ] && { echo "$f"; break; }; done)
114 cv1=FAIL cv2=FAIL co1="no accepted reading to copy" co2="no accepted reading to copy"
115 if [ -n "$base" ]; then
116 rcp=$OUT/q1/$(jq -r .v5_receipt "$base"); rid=$(basename "$rcp" .json)
117 rroot=$(jq -rs --arg r "q1/$rid.json" '[.[] | select(.receipt == $r) | .run_root][0] // empty' "$OUT"/attempts/q1-*.json)
118 CT=$(mktemp -d "${TMPDIR:-/tmp}/smol2q-ctl.XXXXXX")
119 if [ -n "$rroot" ] && [ -f "$rroot/compose.cortex-mark" ]; then
120 cp "$rroot/compose.cortex-mark" "$CT/mark"; printf 'X' | dd of="$CT/mark" bs=1 seek=70 conv=notrunc 2>/dev/null
121 d=$(a1_read "$rcp" "$CT/mark"); [ "$(jq -r .verdict <<<"$d")" = FAIL ] && [ "$(jq -r .mark_well_formed <<<"$d")" = false ] && cv1=PASS
122 co1="a mark with one byte changed (copy of $rid mark) is read as $(jq -r .verdict <<<"$d"), mark_well_formed $(jq -r .mark_well_formed <<<"$d")"
123 jq '(.acceptance_v5.task_quality[] | select(.row == "Q4") | .result) = "FAIL"' "$rcp" >"$CT/rc.json"
124 d=$(a1_read "$CT/rc.json" "$rroot/compose.cortex-mark"); [ "$(jq -r .verdict <<<"$d")" = FAIL ] && [ "$(jq -r .only_these_rows <<<"$d")" = false ] && cv2=PASS
125 co2="a copy of receipt $rid with Q4 set to FAIL is read as $(jq -r .verdict <<<"$d"), only_these_rows $(jq -r .only_these_rows <<<"$d")"
126 fi
127 rm -rf "$CT"
128 fi
129 jq -nc --arg v "$cv1" --arg o "$co1" '{row:"A1-ctl-damaged-mark", rep:1, verdict:$v, outcome:$o, env:"host"}' >>"$OUT/q1-results.jsonl"
130 jq -nc --arg v "$cv2" --arg o "$co2" '{row:"A1-ctl-extra-failing-row", rep:1, verdict:$v, outcome:$o, env:"host"}' >>"$OUT/q1-results.jsonl"
131 echo "Q1 section 4.1 controls: damaged mark $cv1; extra failing row $cv2"
132 fi
133 s=$(bash "$SCORE" "$HERE/q1.decl.json" "$OUT/q1-results.jsonl"); src=$?
134 f=$(jq -c --arg k "$KIND" '. + {kind:$k, results_sha256:"'"$(sha256sum "$OUT/q1-results.jsonl" | cut -d' ' -f1)"'"}' <<<"$s" | name_by_sha "$OUT/scores" q1-score-)
135 echo "Q1 scoring-v5 verdict: $(jq -r .verdict "$OUT/scores/$f") (scores/$f, scorer exit $src)"
136 ;;
137
138_q1_round) # inside the hold: one launch per task, in order, never repeated
139 refuse_unless_digests # recorded in this round's attempt records (issue #240)
140 BASE=$3 OUT=$4 r=$5 TASKS=$6
141 for id in $TASKS; do
142 goal=$(jq -r --arg id "$id" '.tasks[] | select(.id == $id) | .goal' "$NP1/tasks-v5.json")
143 R=$BASE/r$r-$id; t0=$(date -u +%FT%TZ)
144 gpu_env NP1_GOAL="$goal" bash "$NP1/run-campaign.sh" "$R" >"$R.driver.out" 2>&1; drc=$?
145 receipt=null verdict=FAIL outcome="launch failed: no run.json (driver exit $drc)"
146 if [ -f "$R/run.json" ]; then
147 TASK_ID=$id SPEC="smollm2-qualification/ACCEPTANCE-SMOLLM2-Q.md spec_version 1 (rows of next-phase-1/ACCEPTANCE-v5.md)" \
148 bash "$NP1/make-receipt.sh" "$R" "$OUT/q1" "$SC" "$OC" "$OC" 0 "SMOLLM2-Q Q1 $id rep $r ($KIND)" >"$R.receipt.out" 2>&1
149 line=$(head -1 "$R.receipt.out")
150 h=$(sed -n 's/^NEXT_PHASE_1 receipt \([0-9a-f]*\)\.json verdict \([A-Z]*\)$/\1/p' <<<"$line")
151 v=$(sed -n 's/^NEXT_PHASE_1 receipt [0-9a-f]*\.json verdict \([A-Z]*\)$/\1/p' <<<"$line")
152 if [ -n "$h" ]; then receipt="\"q1/$h.json\"" verdict=$v outcome="receipt $h"; else outcome="receipt script failed: $line"; fi
153 # ACCEPTANCE-SMOLLM2-Q section 4.1 (q1_a1_record_mark = record-mark): the v5 receipt stays as it is;
154 # a second record reads it under the six conditions of a1_read.
155 if [ -n "$h" ] && [ "$v" != PASS ] && [ "$A1RULE" = record-mark ]; then
156 rf=$(a1_read "$OUT/q1/$h.json" "$R/compose.cortex-mark" | name_by_sha "$OUT/q1-a1" a1-)
157 verdict=$(jq -r .verdict "$OUT/q1-a1/$rf") outcome="receipt $h (v5 $v); section 4.1 reading q1-a1/$rf $verdict"
158 fi
159 fi
160 # section 8 observations per launch (not scored): each daemon start's KV pool plan line, warm-up line,
161 # VmHWM (from run.json) and any fatal or session-open attempt line
162 memobs=$(for dl in "$R"/daemon-1.log "$R"/daemon-2.log; do [ -f "$dl" ] || continue
163 sed 's/\x1b\[[0-9;]*m//g' "$dl" | jq -Rsc --arg f "$(basename "$dl")" 'split("\n") as $l | {log:$f,
164 kv_pool_plan_line:($l | map(select(test("KV pool: [0-9]+ bytes"))) | .[0] // null),
165 warm_up_line:($l | map(select(test("Warm-up:"))) | .[0] // null),
166 fatal_lines:($l | map(select(test("Fatal:|STRICT_REAL_MODEL_VIOLATION|0x51")))),
167 session_open_attempt_lines:($l | map(select(test("attempt [0-9]+/[0-9]+"))) | length)}'; done | jq -sc .)
168 hwms=$(jq -c '[.daemon[]? | {vmhwm_kb, warm_up_ms}]' "$R/run.json" 2>/dev/null || echo null)
169 a=$(jq -n --arg k "$KIND" --arg id "$id" --argjson rep "$r" --arg root "$R" --argjson drc "$drc" \
170 --argjson receipt "$receipt" --arg v "$verdict" --arg o "$outcome" --arg t0 "$t0" --arg t1 "$(date -u +%FT%TZ)" \
171 --argjson dg "$DIGESTS" --arg tail "$(tail -5 "$R.driver.out" 2>/dev/null)" --arg sc "$SC" --argjson memobs "$memobs" --argjson hwms "${hwms:-null}" \
172 '{gate:"SMOL2Q_Q1_ATTEMPT", kind:$k, sc_commit:$sc, row:$id, rep:$rep, env:"GPU", run_root:$root, driver_exit:$drc,
173 receipt:$receipt, verdict:$v, outcome:$o, started:$t0, ended:$t1, digests_checked_before_round:$dg, driver_tail:$tail, memory_observations:$memobs, daemon_vmhwm:$hwms}' \
174 | name_by_sha "$OUT/attempts" q1-)
175 jq -nc --arg id "$id" --argjson rep "$r" --arg v "$verdict" --arg o "$outcome; attempt $a" \
176 '{row:$id, rep:$rep, verdict:$v, outcome:$o, env:"GPU"}' >>"$OUT/q1-results.jsonl"
177 echo "Q1 $id rep $r: $verdict ($outcome; attempts/$a)"
178 done
179 ;;
180
181q2)
182 BASE=${3:?RUN_BASE} OUT=${4:?OUT}; out_ok "$OUT" || exit 3
183 mkdir -p "$BASE" "$OUT/q2" "$OUT/attempts" "$OUT/scores"; BASE=$(cd "$BASE" && pwd); OUT=$(cd "$OUT" && pwd)
184 if [ -e "$BASE/F0" ] || [ -e "$BASE/cases" ] || [ -e "$OUT/q2-results.jsonl" ]; then
185 echo "REFUSED: Q2 already ran in $BASE or $OUT (never repeated)" >&2; exit 3
186 fi
187 refuse_unless_digests
188 hold 20 "SMOLLM2-Q Q2 fixture F0 ($KIND)" bash "$0" _q2_fixture "$INPUTS" "$BASE" "$OUT"
189 # The cases never touch the GPU (run-faults.sh stops on a non CPU-reference daemon); like
190 # builds, they wait while another lane holds the quiet flag.
191 refuse_unless_digests
192 wait_quiet "Q2 cases"
193 CASES= QREPS=3
194 if [ "$KIND" = TRIAL ]; then CASES=${SMOL2Q_TRIAL_CASES:-} QREPS=${SMOL2Q_TRIAL_REPS:-3}; fi
195 t0=$(date -u +%FT%TZ)
196 # shellcheck disable=SC2086
197 clean_env AIEN_BIN="$(inp cpu_fault_path)" FIX_OLD="$(inp fix_old_dir)" REPS="$QREPS" \
198 bash "$NP2/run-faults.sh" cases "$BASE/F0" "$BASE/cases" $CASES >"$BASE/cases.out" 2>&1; crc=$?
199 mkdir -p "$BASE/cases"; touch "$BASE/cases/results.jsonl"
200 jq -c --slurpfile d "$HERE/q2.decl.json" '. as $r | {row, rep, verdict, outcome, run_root,
201 env: (($d[0].rows | map(select(.row == $r.row)) | .[0].env) // "undeclared")}' "$BASE/cases/results.jsonl" >"$OUT/q2-results.jsonl"
202 FJ=$BASE/F0/fixture.json
203 if [ -f "$FJ" ]; then
204 jq -c '{row:"C3-control", rep:1, env:"GPU",
205 verdict:(if .s3_committed == true and ((.backend // "") | test("OmegaGb10Backend")) then "PASS" else "FAIL" end),
206 outcome:("F0 daemon with AIEN_REQUIRE_BLACKWELL=1: " + (.backend // "none") + "; s3_committed " + (.s3_committed | tostring))}' "$FJ" >>"$OUT/q2-results.jsonl"
207 jq -c '{F0: (.s3_committed == true)}' "$FJ" >"$BASE/external.json"
208 else
209 echo '{"row":"C3-control","rep":1,"env":"GPU","verdict":"NOT_RUN","outcome":"no fixture.json"}' >>"$OUT/q2-results.jsonl"
210 echo '{"F0":false}' >"$BASE/external.json"
211 fi
212 s=$(bash "$SCORE" "$HERE/q2.decl.json" "$OUT/q2-results.jsonl" "$BASE/external.json"); src=$?
213 sf=$(jq -c --arg k "$KIND" --arg h "$(sha256sum "$OUT/q2-results.jsonl" | cut -d' ' -f1)" --slurpfile x "$BASE/external.json" \
214 '. + {kind:$k, results_sha256:$h, external:$x[0]}' <<<"$s" | name_by_sha "$OUT/scores" q2-score-)
215 # NEXT-PHASE-2 v4-format receipt (evidence: builds, link proof, per-run checks, not-proved list)
216 lp() { jq -n --argjson c "$(strings -a "$1" | grep -Eo 'compose\.(candidate\.0|commit|verify)' | sort -u | jq -R . | jq -sc .)" \
217 --argjson g "$(strings -a "$1" | grep -Eo 'NVIDIA_DGX_SPARK_GB10_SM121' | sort -u | jq -R . | jq -sc .)" \
218 --argjson p "$(strings -a "$1" | grep -c reconcile_panic)" --argjson e "$(strings -a "$1" | grep -c reconcile_error)" \
219 '{compose_markers:$c, gpu_markers:$g, reconcile_panic_strings:$p, reconcile_error_strings:$e}'; }
220 jq -n --arg ev "$(inp build_evidence)" --slurpfile cf "$(dirname "$(inp cpu_fault_path)")/build-cpu-fault.json" \
221 '{gpu:{build_evidence:$ev, recipe:"SMOLLM2-Q clean double build (the CAND-4 recipe) (release recipe, scripts/release-build.sh)"}, "cpu-fault":$cf[0]}' >"$BASE/build_env.json"
222 jq -n --argjson g "$(lp "$(inp aien_cli_path)")" --argjson c "$(lp "$(inp cpu_fault_path)")" \
223 '{method:"strings(1) on each binary (NEXT-PHASE-2 link_proof markers)", gpu:$g, "cpu-fault":$c}' >"$BASE/link_proof.json"
224 rr=null
225 if [ -f "$FJ" ] && [ -s "$BASE/cases/results.jsonl" ]; then
226 FIX_OLD="$(inp fix_old_dir)" bash "$NP2/make-receipt.sh" "$BASE/F0" "$BASE/cases" "$OUT/q2" "$SC" "$OC" \
227 "$(inp aien_cli_path)" "$(inp cpu_fault_path)" "$BASE/build_env.json" "$BASE/link_proof.json" \
228 "SMOLLM2-Q Q2 ($KIND): NEXT-PHASE-2 v4 case set on the SMOLLM2-Q binaries; this v4-format verdict is evidence only, the scoring-v5 verdict (ACCEPTANCE-SMOLLM2-Q section 5) decides" \
229 >"$BASE/receipt.out" 2>&1
230 h=$(sed -n 's/^NEXT_PHASE_2 receipt \([0-9a-f]*\)\.json verdict.*/\1/p' "$BASE/receipt.out" | head -1)
231 [ -n "$h" ] && rr="\"q2/$h.json\""
232 fi
233 a=$(jq -n --arg k "$KIND" --arg sc "$SC" --argjson crc "$crc" --arg t0 "$t0" --arg t1 "$(date -u +%FT%TZ)" \
234 --argjson n "$(wc -l <"$BASE/cases/results.jsonl")" --arg h "$(sha256sum "$BASE/cases/results.jsonl" | cut -d' ' -f1)" \
235 --argjson rr "$rr" --arg sf "scores/$sf" --argjson dg "$DIGESTS" --arg tail "$(tail -3 "$BASE/cases.out")" \
236 '{gate:"SMOL2Q_Q2_CASES", kind:$k, sc_commit:$sc, binary:"cpu_fault", cases_exit:$crc, started:$t0, ended:$t1,
237 result_lines:$n, results_sha256:$h, np2_receipt:$rr, score:$sf, digests_checked_before_cases:$dg, tail:$tail}' \
238 | name_by_sha "$OUT/attempts" q2-cases-)
239 echo "Q2 cases exit $crc; scoring-v5 verdict: $(jq -r .verdict "$OUT/scores/$sf") (scores/$sf, scorer exit $src); attempts/$a; NEXT-PHASE-2 receipt $rr"
240 ;;
241
242_q2_fixture) # inside the hold: F0 on the production binary, once
243 refuse_unless_digests # recorded in the q2-fixture attempt record (issue #240)
244 BASE=$3 OUT=$4; t0=$(date -u +%FT%TZ)
245 gpu_env bash "$NP2/run-faults.sh" fixture "$BASE/F0" >"$BASE/F0.out" 2>&1; frc=$?
246 fj=null; [ -f "$BASE/F0/fixture.json" ] && fj=$(jq -c . "$BASE/F0/fixture.json")
247 a=$(jq -n --arg k "$KIND" --arg sc "$SC" --argjson frc "$frc" --argjson fj "$fj" --arg t0 "$t0" --arg t1 "$(date -u +%FT%TZ)" \
248 --argjson dg "$DIGESTS" --arg tail "$(tail -5 "$BASE/F0.out")" \
249 '{gate:"SMOL2Q_Q2_FIXTURE", kind:$k, sc_commit:$sc, binary:"aien_cli", env:"GPU", exit:$frc, fixture:$fj,
250 valid:(($fj // {}).s3_committed == true), started:$t0, ended:$t1, digests:$dg, tail:$tail}' | name_by_sha "$OUT/attempts" q2-fixture-)
251 echo "Q2 F0 exit $frc valid $(jq -r '.s3_committed // false' "$BASE/F0/fixture.json" 2>/dev/null || echo false) (attempts/$a)"
252 ;;
253q2w)
254 BASE=${3:?RUN_BASE} OUT=${4:?OUT}; out_ok "$OUT" || exit 3
255 mkdir -p "$OUT/q2w" "$OUT/attempts" "$OUT/scores"; BASE=$(cd "$BASE" && pwd); OUT=$(cd "$OUT" && pwd)
256 [ -f "$BASE/F0/fixture.json" ] || { echo "REFUSED: no F0 under $BASE (run q2 first)" >&2; exit 3; }
257 [ -e "$BASE/windows" ] && { echo "REFUSED: the window tests already ran into $BASE/windows (never repeated)" >&2; exit 3; }
258 refuse_unless_digests
259 clean_env AIEN_BIN="$(inp cpu_fault_path)" bash "$HERE/run-windows.sh" "$BASE/F0" "$BASE/windows" >"$BASE/windows.out" 2>&1; wrc=$?
260 refuse_unless_digests
261 [ -f "$BASE/windows/results.jsonl" ] || : >"$BASE/windows/results.jsonl"
262 cp "$BASE/windows/results.jsonl" "$OUT/q2w-results.jsonl"
263 s=$(bash "$SCORE" "$HERE/windows.decl.json" "$OUT/q2w-results.jsonl"); src=$?
264 f=$(jq -c --arg k "$KIND" --arg h "$(sha256sum "$OUT/q2w-results.jsonl" | cut -d' ' -f1)" '. + {kind:$k, results_sha256:$h}' <<<"$s" | name_by_sha "$OUT/scores" q2w-score-)
265 a=$(jq -n --arg k "$KIND" --arg sc "$SC" --argjson wrc "$wrc" --arg v "$(jq -r .verdict "$OUT/scores/$f")" --argjson dg "$DIGESTS" --arg tail "$(tail -8 "$BASE/windows.out")" \
266 '{gate:"SMOL2Q_Q2W_ATTEMPT", kind:$k, sc_commit:$sc, binary:"cpu_fault", env:"CPU fault injection", exit:$wrc, verdict:$v, digests:$dg, tail:$tail}' | name_by_sha "$OUT/attempts" q2w-)
267 echo "Q2w (coverage cases 2 and 5) scoring-v5 verdict: $(jq -r .verdict "$OUT/scores/$f") (scores/$f, scorer exit $src; attempts/$a)"
268 ;;
269mem) # section 8 observations (closes #236): one SmolLM2 daemon start and one Llama-3.2-1B control start
270 BASE=${3:?RUN_BASE} OUT=${4:?OUT}; out_ok "$OUT" || exit 3
271 mkdir -p "$BASE" "$OUT/mem"; BASE=$(cd "$BASE" && pwd); OUT=$(cd "$OUT" && pwd)
272 [ -e "$BASE/mem" ] && { echo "REFUSED: the memory observations already ran into $BASE/mem (never repeated)" >&2; exit 3; }
273 refuse_unless_digests
274 hold 10 "SMOLLM2-Q memory observations ($KIND)" bash "$0" _mem "$INPUTS" "$BASE" "$OUT"
275 ;;
276
277_mem) # inside the hold: SmolLM2 start, then the Llama control start, same binary, once each
278 refuse_unless_digests
279 BASE=$3 OUT=$4; mkdir -p "$BASE/mem"
280 LD=$(inp llama_control_dir) bad=0
281 dg llama_control/model.safetensors "$(readlink -f "$LD/model.safetensors")" "$(inp llama_control_safetensors)" || bad=1
282 dg llama_control/config.json "$(readlink -f "$LD/config.json")" "$(inp llama_control_config)" || bad=1
283 [ $bad = 0 ] || { echo "REFUSED: Llama control files differ from CAND-4 (no start)" >&2; exit 3; }
284 # unified memory (DGX Spark known issues): read /proc/meminfo, not nvidia-smi or cudaMemGetInfo
285 meminfo() { awk '/^(MemTotal|MemAvailable|SwapFree|HugePages_Total|HugePages_Free|Hugepagesize):/ {printf "%s%s ", $1, $2}' /proc/meminfo; }
286 devholders() { for p in /proc/[0-9]*; do ls -l "$p/fd" 2>/dev/null | grep -q /dev/nvidia && printf '%s ' "${p#/proc/}"; done; }
287 start_one() { # NAME MODEL_DIR -> one observation record (not scored)
288 local name=$1 d=$2 R=$BASE/mem/$1 dp i t0 st=null hwm= wu plan chk fatal retries ex g0 h0
289 mkdir -p "$R/compose" "$R/prov" "$R/state"; t0=$(date -u +%FT%TZ); g0=$(meminfo); h0=$(devholders)
290 # exec: $! must be the daemon itself, not a subshell running the clean_env function
291 # (first mem run read VmHWM of that subshell: 2716 kB; disclosed in the verdict)
292 exec_clean() { exec env -i PATH="$PATH" HOME="$HOME" USER="${USER:-}" LANG="${LANG:-C.UTF-8}" "$@"; }
293 # sc#328: the daemon requires the approval desk key.
294 AIEN_COMPOSE_DIR="$R/compose" "$(inp aien_cli_path)" compose desk-key --create 1 >/dev/null
295 exec_clean AIEN_COMPOSE_DIR="$R/compose" AIEN_PROVENANCE_DIR="$R/prov" AIEN_RUNTIME_SOCK="$R/aien.sock" \
296 AIEN_RUNTIME_STATE_DIR="$R/state" AIEN_REQUIRE_CHECKPOINT=1 AIEN_MODEL_PATH="$d/model.safetensors" \
297 AIEN_TOKENIZER_PATH="$d/tokenizer.json" AIEN_COMPOSE_MAX_TOKENS="$(inp max_tokens)" AIEN_REQUIRE_BLACKWELL=1 \
298 setsid "$(inp aien_cli_path)" daemon >"$R/daemon.log" 2>&1 </dev/null &
299 dp=$! i=0
300 until grep -q 'Warm-up:\|Fatal:\|STRICT_REAL_MODEL_VIOLATION' "$R/daemon.log" || ! kill -0 "$dp" 2>/dev/null || [ $i -gt 600 ]; do sleep 0.5; i=$((i + 1)); done
301 sleep 1
302 if kill -0 "$dp" 2>/dev/null; then
303 st=$( { echo "measured: $(tr "\\0" " " <"/proc/$dp/cmdline")"; grep -E "^(VmHWM|VmRSS|VmSize)" "/proc/$dp/status"; } | jq -R . | jq -sc .)
304 hwm=$(awk '/^VmHWM/ {print $2}' "/proc/$dp/status")
305 clean_env AIEN_RUNTIME_SOCK="$R/aien.sock" "$(inp aien_cli_path)" compose shutdown >"$R/shutdown.out" 2>&1
306 i=0; while kill -0 "$dp" 2>/dev/null && [ $i -lt 120 ]; do sleep 0.5; i=$((i + 1)); done
307 fi
308 kill -0 "$dp" 2>/dev/null && ex="still running after the shutdown request (left for the operator, never killed)" || ex="exited"
309 plan=$(sed 's/\x1b\[[0-9;]*m//g' "$R/daemon.log" | grep -m1 'KV pool: [0-9]* bytes' | sed 's/^ *//')
310 chk=$(sed 's/\x1b\[[0-9;]*m//g' "$R/daemon.log" | grep -m1 'KV pool memory check' | sed 's/^ *//')
311 wu=$(sed 's/\x1b\[[0-9;]*m//g' "$R/daemon.log" | grep -m1 'Warm-up:' | sed 's/^ *//')
312 fatal=$(sed 's/\x1b\[[0-9;]*m//g' "$R/daemon.log" | grep -m3 -E 'Fatal:|STRICT_REAL_MODEL_VIOLATION|0x51' | jq -R . | jq -sc .)
313 retries=$(sed 's/\x1b\[[0-9;]*m//g' "$R/daemon.log" | grep -cE 'attempt [0-9]+/[0-9]+')
314 jq -n --arg k "$KIND" --arg sc "$SC" --arg n "$name" --arg d "$d" --arg t0 "$t0" --arg t1 "$(date -u +%FT%TZ)" \
315 --arg g0 "$g0" --arg h0 "$h0" --arg plan "$plan" --arg chk "$chk" --arg wu "$wu" --argjson fatal "$fatal" --argjson st "$st" \
316 --argjson hwm "${hwm:-null}" --argjson rt "$retries" --arg ex "$ex" --argjson dg "$DIGESTS" \
317 --arg log "$(sha256sum "$R/daemon.log" | cut -d' ' -f1)" \
318 '{gate:"SMOL2Q_MEM_OBSERVATION", kind:$k, sc_commit:$sc, model:$n, model_dir:$d, started:$t0, ended:$t1,
319 meminfo_before:$g0, gpu_device_holders_before:$h0, kv_pool_plan_line:$plan, kv_pool_memory_check_line:$chk,
320 warm_up_line:$wu, fatal_lines:$fatal, session_open_attempt_lines:$rt, proc_status_after_warm_up:$st,
321 vmhwm_kb_after_warm_up:$hwm, daemon_end:$ex, daemon_log_sha256:$log, digests:$dg, scored:false}' | name_by_sha "$OUT/mem" mem-
322 }
323 for m in "smollm2 $MD" "llama-control $LD"; do
324 set -- $m; f=$(start_one "$1" "$2")
325 echo "memory observation $1: $(jq -r '.kv_pool_plan_line' "$OUT/mem/$f" | cut -c1-60); VmHWM $(jq -r .vmhwm_kb_after_warm_up "$OUT/mem/$f") kB (mem/$f)"
326 done
327 ;;
328*) echo "unknown command $CMD" >&2; exit 2;;
329esac