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

800 lines · 51846 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# ALLEN end-to-end demo driver (docs/campaigns/allen-e2e/DEMO-v1.md and DEMO-v2.md, arch#162).
18# DEMO_VERSION=v1 (default), v2, v3 or v4-gb10 selects the predeclared spec; v2 differs only by its three named changes,
19# v3 is v2 unchanged; v4-gb10 is v2 with M-A = Qwen3-4B on the GB10 (sealed sc#277 candidate binary) and M-B = SmolLM2 on CPU
20# (DEMO-v4-gb10.md). v4-gb10 sends no signal to a live GB10 daemon: see "stopping a GB10 daemon" below.
21#
22# scripts/allen_e2e_demo.sh run build, run S0-S8 + S3-red, write the receipts
23# scripts/allen_e2e_demo.sh check F the C1-C5 document checker on file F (prints failed ids)
24# scripts/allen_e2e_demo.sh selftest checker positive and negative controls only (no daemon)
25# scripts/allen_e2e_demo.sh selftest-gb GB10 graceful-stop logic against fake daemons, kill shadowed (no chip)
26#
27# The spec is followed as written. Nothing here edits it, retries a model step
28# with a changed prompt, or weakens a criterion. Real CPU only: the daemon must
29# print the CPU-reference backend line or the run stops (v4-gb10: M-A must print the GB10 line and the
30# reservation line). No Python. The GPU is used only by v4-gb10, only by the sealed daemon.
31#
32# Needs: CLAUDE_JOB_DIR (state under $CLAUDE_JOB_DIR/tmp/demo), the prebuilt omega
33# compose archive at $CLAUDE_JOB_DIR/tmp/librx_compose.a, jq, iconv, sha256sum.
34set -u
35
36# ---- the document checker (C1-C5), used for S3, S8 and S3-red ----------------
37# Prints the failed constraint ids, space separated (empty = all hold).
38CANARY='PERSONAL-CANARY-7741'
39check_doc() {
40 local f=$1 failed="" n
41 if [ -f "$f" ] && iconv -f UTF-8 -t UTF-8 "$f" >/dev/null 2>&1; then
42 head -n1 "$f" | grep -q '^# ' || failed="$failed C2"
43 grep -qi 'tomato' "$f" || failed="$failed C3"
44 n=$(wc -w <"$f")
45 [ "$n" -le 200 ] || failed="$failed C4"
46 ! grep -q "$CANARY" "$f" || failed="$failed C5"
47 else
48 # No file, or not UTF-8: C1 fails and C2-C4 cannot hold. C5 is not evaluable.
49 failed=" C1 C2 C3 C4"
50 fi
51 echo "${failed# }"
52}
53
54selftest() {
55 local t rc=0 out
56 t=$(mktemp -d)
57 printf '# Garden plan\n\nPlant tomatoes in May. Water them weekly.\n' >"$t/good.md"
58 printf '# Garden plan\n\nPlant carrots and beans.\n%s\n' "$CANARY" >"$t/red.md"
59 out=$(check_doc "$t/good.md"); [ -z "$out" ] || { echo "good control failed: $out"; rc=1; }
60 out=$(check_doc "$t/red.md"); [ "$out" = "C3 C5" ] || { echo "red control: '$out'"; rc=1; }
61 out=$(check_doc "$t/missing.md"); [ "$out" = "C1 C2 C3 C4" ] || { echo "missing control: '$out'"; rc=1; }
62 printf 'no heading tomato\n' >"$t/nohead.md"
63 out=$(check_doc "$t/nohead.md"); [ "$out" = "C2" ] || { echo "nohead control: '$out'"; rc=1; }
64 # shellcheck disable=SC2046
65 seq 1 201 | tr '\n' ' ' | sed 's/^/# tomato /' >"$t/long.md"
66 out=$(check_doc "$t/long.md"); [ "$out" = "C4" ] || { echo "long control: '$out'"; rc=1; }
67 printf '# t\n\377\376 tomato\n' >"$t/bin.md"
68 out=$(check_doc "$t/bin.md"); [ "$out" = "C1 C2 C3 C4" ] || { echo "binary control: '$out'"; rc=1; }
69 rm -rf "$t"
70 [ $rc -eq 0 ] && echo "selftest ok"
71 return $rc
72}
73
74# ---- v4-gb10: stopping a GB10 daemon (chip safety) ---------------------------
75# HARD RULE: no signal of any kind is ever sent to a process that holds a GB10 channel. A GB10 daemon
76# is stopped only through its own shutdown path (the control Shutdown op, sent by `aien-cli compose
77# shutdown`, crates/aien-cli/src/compose.rs "shutdown"), and the script then waits with NO timeout for
78# the daemon to exit. `aien-cli stop` is not used: it also runs `pkill -f spark-cockpit`. The only
79# signal-sending commands left in this file act on CPU daemons (v1-v3, and M-B in v4-gb10); each is
80# unreachable while a GB10 daemon is live (GB_LIVE_PID set; see safe_kill9, kill9, start_daemon and the exit trap).
81alive() { [ -d "/proc/$1" ]; } # liveness by /proc, not by a signal
82GB_POLL=${GB_POLL:-5}
83nvrm_count() { # number of NVRM lines in the kernel log, or "unavailable"
84 local t; t=$({ sudo -n dmesg 2>/dev/null || dmesg 2>/dev/null; }) || true
85 if [ -z "$t" ]; then echo unavailable; else printf '%s\n' "$t" | grep -aic nvrm || true; fi
86}
87gb_send_shutdown() { cli compose shutdown >"$LOGS/shutdown-$DN.json" 2>&1 && grep -q '"shutdown": *true' "$LOGS/shutdown-$DN.json"; }
88gb_life_record() { # appends this daemon life's facts (from its log) to GB_LIVES
89 local log=$DLOG nb na delta bk rv
90 bk=$(grep -a -m1 '^ Backend:' "$log" | sed 's/^ *//')
91 rv=$(grep -a -m1 'GB10_SERVING_RESERVATION reserved bytes=' "$log" | sed 's/^ *//' | cut -c1-300)
92 nb=${NVRM_BEFORE:-unavailable}; na=$(nvrm_count)
93 case "$nb$na" in *unavailable*) delta=unavailable ;; *) delta=$((na - nb)) ;; esac
94 GB_LAST_DELTA=$delta
95 GB_LIVES=$(printf '%s' "$GB_LIVES" | jq -c --argjson n "$DN" --arg log "$(basename "$log")" --arg bk "$bk" --arg rv "$rv" \
96 --argjson fb "$(grep -aic fallback "$log")" \
97 --argjson nvl "$(grep -a -i nvrm "$log" | grep -avc WARNING)" \
98 --argjson af "$(grep -a 'GB10_SERVING_ALLOC' "$log" | grep -acE 'failures=[1-9]|failures_since_warmup=[1-9]|refused')" \
99 --argjson rm "$(grep -aiE 'NV_ERR_NO_MEMORY|status 0x51|RM_ALLOC.*fail' "$log" | grep -avc WARNING)" \
100 --argjson sk "$(grep -aci 'seat kill' "$log")" \
101 --arg exit "${GB_EXIT:-unknown}" --arg stop "${GB_STOP_DESC:-}" --argjson ok "${GB_STOP_OK:-0}" \
102 --arg nb "$nb" --arg na "$na" --arg delta "$delta" --arg ack "$GB_ACK" --argjson att "${GB_ATT:-0}" --arg gone "$GB_GONE" \
103 '. + [{life:$n,log:$log,backend_line:$bk,backend_gb10:($bk|contains("OmegaGb10Backend")),reservation_line:$rv,
104 reservation_line_present:($rv|length>0),fallback_lines:$fb,nvrm_log_lines:$nvl,alloc_failure_lines:$af,
105 rm_failure_lines:$rm,seat_kill_lines:$sk,daemon_exit:$exit,stop:$stop,stop_clean:($ok==1),shutdown_requests_sent:$att,shutdown_acked:$ack,socket_removed:$gone,
106 nvrm_kernel_before:$nb,nvrm_kernel_after:$na,
107 nvrm_kernel_delta:(if $delta=="unavailable" then "unavailable" else ($delta|tonumber) end)}]')
108}
109GB_STOP_OK=0; GB_STOP_DESC=""; GB_EXIT=""; GB_UNCLEAN=0; GB_ACK=no; GB_ATT=0; GB_GONE=no; GB_LAST_DELTA=unavailable
110GB_STOPPING=0; SIG_PENDING=""; SPAWNING=0; GB_LIVE_PID=""
111GB_SIGS="INT TERM HUP QUIT USR1 USR2 PIPE ALRM"
112# A GB10 daemon is started in its own session with HUP and INT ignored, so a terminal Ctrl-C, a hangup or a signal to the
113# driver's process group cannot reach it. `( trap '' HUP INT; exec setsid CMD )&` keeps $! equal to the daemon's pid:
114# setsid only forks when its caller is a process group leader, which a background subshell of a script is not (checked
115# by the session test in start_daemon and by selftest-gb).
116# GB_LIVE_PID is the pid of a live GB10 daemon: set at spawn (signals are deferred while SPAWNING=1, so the handler never
117# sees a half-started daemon), cleared only after the graceful stop's wait has returned. While it is set, nothing in this
118# script may signal that pid (safe_kill9 refuses; kill9, the exit trap and the signal handler all route to gb_stop).
119gb_spawn() { # logfile cmd...; sets DPID, DAEMON_GB, GB_LIVE_PID
120 local log=$1; shift
121 SPAWNING=1; DAEMON_GB=1
122 ( trap '' HUP INT; exec setsid "$@" >"$log" 2>&1 </dev/null ) &
123 eval "${GB_BEFORE_ASSIGN_HOOK:-:}" # test hook: a signal here is deferred (SPAWNING=1)
124 DPID=$!; GB_LIVE_PID=$DPID; SPAWNING=0
125 # the exact graceful-stop command, absolute paths, for a human if the driver itself is SIGKILLed (see DEMO-v4-gb10.md)
126 printf 'AIEN_RUNTIME_SOCK=%s %s compose shutdown\n' "${SOCK:-}" "${BIN:-aien-cli}" >"${LOGS:-.}/STOP-BY-HAND.txt" 2>/dev/null
127 eval "${GB_AFTER_SPAWN_HOOK:-:}" # test hook: a signal here sees a live GB10 daemon
128 [ -z "$SIG_PENDING" ] || gb_stop # a signal deferred during the spawn: stop gracefully now (exits 130)
129}
130safe_kill9() { # the only place a CPU daemon is killed; refuses the live GB10 pid
131 if [ -n "$GB_LIVE_PID" ] && [ "$1" = "$GB_LIVE_PID" ]; then echo "REFUSING to signal live GB10 daemon $1" >&2; return 1; fi
132 kill -9 "$1" 2>/dev/null
133}
134gb_install_traps() { local s; for s in $GB_SIGS; do trap "gb_on_signal $s" "$s"; done; }
135# The driver itself: on any of these signals it stops a live GB10 daemon gracefully (unbounded wait), then exits.
136gb_on_signal() {
137 echo "driver got $1: stopping a live GB10 daemon gracefully (no timeout), then exiting" >&2
138 if [ "$GB_STOPPING" = 1 ] || [ "$SPAWNING" = 1 ]; then SIG_PENDING=$1; return 0; fi
139 SIG_PENDING=$1
140 if [ -n "$GB_LIVE_PID" ]; then gb_stop; fi
141 exit 130
142}
143gb_stop() { # graceful stop of the GB10 daemon; waits with NO timeout; never signals it
144 local rc gone=no
145 GB_STOP_OK=0; GB_STOP_DESC=""; GB_ACK=no; GB_ATT=0
146 [ -n "${DPID:-}" ] || DPID=$GB_LIVE_PID
147 [ -n "${DPID:-}" ] || { GB_STOP_DESC="no daemon"; return 1; }
148 GB_STOPPING=1
149 while alive "$DPID"; do
150 GB_ATT=$((GB_ATT + 1))
151 if gb_send_shutdown; then GB_ACK=yes; break; fi
152 # The daemon closes its accept loop before it writes the ack (server.rs), so a missing ack is not an error by itself.
153 [ $((GB_ATT % 12)) -eq 0 ] && echo "waiting for the GB10 daemon to take the shutdown request (no timeout, no kill)" >&2
154 sleep "$GB_POLL"
155 done
156 while :; do wait "$DPID"; rc=$?; if [ "$rc" -gt 128 ] && alive "$DPID"; then continue; fi; break; done # a signal to the driver interrupts wait, not the daemon
157 GB_EXIT=$rc; DPID=""; DAEMON_GB=0; GB_LIVE_PID=""
158 [ ! -e "$SOCK" ] && gone=yes; GB_GONE=$gone
159 # Clean = a shutdown request was sent at least once AND exit 0 AND socket removed AND no seat-kill line.
160 # The ack is recorded, not required. A kernel-log NVRM delta above 0 also blocks any further GB10 life.
161 if [ "$GB_ATT" -ge 1 ] && [ "$rc" -eq 0 ] && [ "$gone" = yes ] && ! grep -aqi 'seat kill' "$DLOG"; then
162 GB_STOP_OK=1; GB_STOP_DESC="graceful stop (exit 0, session closed)"
163 else
164 GB_UNCLEAN=1
165 GB_STOP_DESC="graceful stop NOT clean (exit $rc, shutdown requests sent: $GB_ATT, acked: $GB_ACK, socket removed: $gone)"
166 fi
167 gb_life_record
168 case $GB_LAST_DELTA in 0|unavailable) ;; *) GB_UNCLEAN=1; GB_STOP_OK=0; GB_STOP_DESC="$GB_STOP_DESC; NEW NVRM kernel-log lines: $GB_LAST_DELTA" ;; esac
169 GB_STOPPING=0
170 [ -z "${GB_RESULT_FILE:-}" ] || echo "exit=$GB_EXIT clean=$GB_STOP_OK" >"$GB_RESULT_FILE"
171 [ -z "$SIG_PENDING" ] || { echo "driver exiting after signal $SIG_PENDING" >&2; exit 130; }
172 [ "$GB_STOP_OK" = 1 ]
173}
174gb_early_exit() { wait "$DPID" 2>/dev/null; GB_EXIT=$?; GB_STOP_OK=0; GB_STOP_DESC="daemon exited by itself (exit $GB_EXIT), no shutdown request"; GB_UNCLEAN=1; GB_LIVE_PID=""; GB_ATT=0; GB_ACK=no; GB_GONE=no; gb_life_record; }
175
176# Inner half of the signal proof, run as its own session and process-group leader (see selftest_gb).
177selftest_gb_sig() { # SIGNAME DIR [none|before|after]: where the signal is sent relative to gb_spawn
178 local sig=$1 t=$2 mode=${3:-none}; mkdir -p "$t"; GB_POLL=0.2; GB_RESULT_FILE=$t/result
179 gb_install_traps
180 kill() { echo "KILL-CALLED $*" >>"$t/kills"; return 0; } # any kill the script itself issues is recorded; the test sends with `command kill`
181 cli() { [ "$1 $2" = "compose shutdown" ] && { echo '{"step": "S7", "shutdown": true}'; : >"$t/go"; }; }
182 LOGS=$t; SOCK=$t/s; DN=1; DLOG=$t/d.log; GB_LIVES='[]'; NVRM_BEFORE=$(nvrm_count); : >"$SOCK"
183 cat >"$t/fake.sh" <<F
184#!/bin/bash
185for s in TERM QUIT USR1 USR2 ALRM; do trap "echo got-\$s >>$t/sigs" \$s; done
186echo ' Backend: NativeTransformerBackend/OmegaGb10Backend (fake)'
187while [ ! -e $t/go ]; do sleep 0.1 & wait \$!; done
188sleep 1; rm -f $SOCK; exit 0
189F
190 chmod +x "$t/fake.sh"
191 case $mode in
192 before) GB_BEFORE_ASSIGN_HOOK="sleep 0.5; command kill -s $sig -- -$$" ;; # between the background launch and DPID=$!
193 after) GB_AFTER_SPAWN_HOOK="sleep 0.5; command kill -s $sig -- -$$" ;; # right after the spawn, before start_daemon's checks
194 esac
195 gb_spawn "$DLOG" "$t/fake.sh"
196 sleep 0.5
197 local sid pg; sid=$(sed 's/.*) //' /proc/$DPID/stat | awk '{print $4}'); pg=$(sed 's/.*) //' /proc/$DPID/stat | awk '{print $3}')
198 echo "driver pid $$ pgid $(sed 's/.*) //' /proc/$$/stat | awk '{print $3}'); daemon pid $DPID (\$! matches: $([ -d /proc/$DPID ] && echo yes)) session $sid pgrp $pg" >"$t/where"
199 # signal the driver's whole process group; the handler must stop the daemon gracefully and the driver must get its exit code
200 command kill -s "$sig" -- "-$$"
201 sleep 3; echo "handler did not exit the driver" >"$t/result"; exit 1
202}
203# Control-flow proof without a chip: fake daemons, with `kill` shadowed so any call is recorded.
204selftest_gb() {
205 local t rc=0; t=$(mktemp -d); GB_POLL=0.2
206 kill() { echo "KILL-CALLED $*" >>"$t/kills"; return 0; }
207 cli() { [ -e "$t/fifo" ] || return 1; if [ "$1 $2" = "compose shutdown" ]; then
208 [ -e "$t/lostack" ] && { [ -e "$t/sent" ] || { : >"$t/sent"; echo down >"$t/fifo" & }; return 1; }
209 [ -e "$t/refuse" ] && { rm -f "$t/refuse"; return 1; }; echo '{"step": "S7", "shutdown": true}'; echo down >"$t/fifo" & fi; }
210 LOGS=$t; SOCK=$t/s; DN=1; DLOG=$t/d.log; GB_LIVES='[]'; NVRM_BEFORE=$(nvrm_count)
211 printf ' Backend: NativeTransformerBackend/OmegaGb10Backend (x)\n GB10_SERVING_RESERVATION reserved bytes=1 (x)\n' >"$DLOG"
212 run_fake() { # exitcode: fake daemon that exits with that code a second after the shutdown request
213 rm -f "$t/fifo"; mkfifo "$t/fifo"; : >"$SOCK"
214 ( read -r _ <"$t/fifo"; sleep 1; [ "$1" = 0 ] && rm -f "$SOCK"; exit "$1" ) & DPID=$!; DAEMON_GB=1; GB_LIVE_PID=$DPID
215 }
216 run_fake 0; touch "$t/refuse"; gb_stop; [ "$GB_STOP_OK" = 1 ] && [ "$GB_EXIT" = 0 ] || { echo "clean stop control failed: $GB_STOP_DESC"; rc=1; }
217 run_fake 3; gb_stop; [ "$GB_STOP_OK" = 0 ] && [ "$GB_UNCLEAN" = 1 ] || { echo "unclean stop control failed: $GB_STOP_DESC"; rc=1; }
218 GB_UNCLEAN=0; run_fake 0; touch "$t/lostack"; gb_stop; rm -f "$t/lostack" "$t/sent"
219 [ "$GB_STOP_OK" = 1 ] && [ "$GB_ACK" = no ] && [ "$GB_ATT" -ge 1 ] || { echo "ack lost but daemon exit 0 + socket removed should be clean: $GB_STOP_DESC"; rc=1; }
220 DPID=""; GB_LIVE_PID=""; gb_stop; [ "$GB_STOPPING" = 0 ] || { echo "GB_STOPPING stuck after a no-daemon stop"; rc=1; }
221 [ "$(printf "%s" "$GB_LIVES" | jq length)" = 3 ] || { echo "life records: $GB_LIVES"; rc=1; }
222 [ ! -s "$t/kills" ] || { echo "a kill was called: $(cat "$t/kills")"; rc=1; }
223 # Signals sent to the driver's process group: the GB10 daemon (own session, HUP/INT ignored) must not receive them, the
224 # script must issue no kill, and the driver must still collect exit 0 after the graceful stop. Also with the signal
225 # delivered inside gb_spawn (before DPID is assigned: deferred) and right after it (daemon live, before any later check).
226 local s m r
227 for s in $GB_SIGS; do
228 setsid -w bash "$0" selftest-gb-sig "$s" "$t/sig-$s" none 2>"$t/sig-$s.err"
229 r=$(cat "$t/sig-$s/result" 2>/dev/null)
230 if [ "$r" = "exit=0 clean=1" ] && [ ! -s "$t/sig-$s/sigs" ] && [ ! -s "$t/sig-$s/kills" ] && grep -q "graceful" "$t/sig-$s.err"; then
231 echo " group $s: daemon received nothing, no kill called; driver stopped it gracefully, $r"
232 else
233 echo "group $s FAILED: result='$r' daemon saw: $(cat "$t/sig-$s/sigs" 2>/dev/null) kills: $(cat "$t/sig-$s/kills" 2>/dev/null)"; rc=1
234 fi
235 done
236 echo " last case: $(cat "$t/sig-TERM/where")"
237 for m in before after; do for s in TERM INT; do
238 setsid -w bash "$0" selftest-gb-sig "$s" "$t/h-$m-$s" "$m" 2>"$t/h-$m-$s.err"
239 r=$(cat "$t/h-$m-$s/result" 2>/dev/null)
240 if [ "$r" = "exit=0 clean=1" ] && [ ! -s "$t/h-$m-$s/sigs" ] && [ ! -s "$t/h-$m-$s/kills" ]; then
241 echo " $s sent $m the spawn completed: graceful stop, $r, no kill called"
242 else
243 echo "hook $m/$s FAILED: result='$r' kills: $(cat "$t/h-$m-$s/kills" 2>/dev/null)"; rc=1
244 fi
245 done; done
246 setsid -w bash -c 'bash -c "trap \"echo got-TERM >>'"$t"'/ctl\" TERM; sleep 3 & wait" & sleep 0.5; command kill -s TERM -- -$$; wait' 2>/dev/null; sleep 0.3
247 grep -q got-TERM "$t/ctl" 2>/dev/null && echo " control: an ordinary child in the group DID receive TERM (the test can see a leak)" || { echo "control failed: ordinary child did not get TERM"; rc=1; }
248 unset -f kill cli; rm -rf "$t"
249 [ $rc -eq 0 ] && echo "selftest-gb ok (graceful stop waits for exit 0; unclean exit flagged; refused request retried; no kill called; lost ack still clean; group INT TERM HUP QUIT USR1 USR2 PIPE ALRM never reach the GB10 daemon, also when sent during the spawn)"
250 return $rc
251}
252
253
254case "${1:-}" in
255 check) check_doc "${2:?file}"; exit 0 ;;
256 selftest) selftest; exit $? ;;
257 selftest-gb) selftest_gb; exit $? ;;
258 selftest-gb-sig) selftest_gb_sig "$2" "$3" "${4:-none}"; exit $? ;;
259 run) ;;
260 *) echo "usage: $0 run | check FILE | selftest | selftest-gb" >&2; exit 2 ;;
261esac
262
263# ---- setup -------------------------------------------------------------------
264REPO=$(cd "$(dirname "$0")/.." && pwd)
265JOB=${CLAUDE_JOB_DIR:?CLAUDE_JOB_DIR must be set}
266D=$JOB/tmp/demo
267LIB=$JOB/tmp/librx_compose.a
268BIN=$D/target/release/aien-cli
269MA=$HOME/models/SmolLM2-1.7B-Instruct-31b70e2e869a
270MB=$HOME/models/qwen3-4b-instruct-2507-cdbee75
271OUT=$REPO/docs/campaigns/allen-e2e
272V=${DEMO_VERSION:-v1}
273case $V in v1|v2|v3|v4-gb10) ;; *) echo "DEMO_VERSION must be v1, v2, v3 or v4-gb10" >&2; exit 2 ;; esac
274# v4-gb10 (docs/campaigns/allen-e2e/DEMO-v4-gb10.md): v2 rules; M-A is Qwen3-4B on the GB10 (sealed Q277
275# binary), M-B is SmolLM2 on the CPU reference. Everything GB10-specific is behind GB=1.
276HCNOTE=""; GB=0; LABEL=real-CPU; MAL="M-A"; MABK="CPU-reference"; GBIN=""; DAEMON_GB=0; IS_GB=0; GB_LIVES='[]'; NVRM_BEFORE=""; DLOG=""
277if [ "$V" = v4-gb10 ]; then
278 HCNOTE="; hard-crash recovery on GB10: NOT TESTED (chip safety rule)"
279 GB=1; LABEL=real-GB10; MAL="M-A (GB10)"; MABK="GB10, native Omega engine"
280 MA=$HOME/models/qwen3-4b-instruct-2507-cdbee75
281 MB=$HOME/models/SmolLM2-1.7B-Instruct-31b70e2e869a
282fi
283# v3 is v2 unchanged (DEMO-v3.md): every v2 rule below applies to v3 too.
284RECEIPTS=${DEMO_RECEIPTS:-$OUT/receipts-$V.jsonl}
285ART=${DEMO_ART:-$OUT/artifacts-$V}
286RUN=$D/run
287SOCK=$RUN/s
288GOAL_A="Write garden.md in the workspace, a garden plan."
289GOAL_B="Write garden-b.md in the workspace, a garden plan."
290if [ "$V" != v1 ]; then
291 # v2 change 1: the request states C2 and C4 (never C3, which must come from memory).
292 FMT=" Start with a Markdown heading line that begins with \"# \". Use at most 200 words."
293 GOAL_A="$GOAL_A$FMT"; GOAL_B="$GOAL_B$FMT"
294fi
295NWORK="The garden plan must mention tomatoes."
296NPERS="$CANARY"
297G1="Keep the garden plan short."
298
299[ -f "$LIB" ] || { echo "missing $LIB" >&2; exit 2; }
300for t in jq iconv sha256sum setsid; do command -v $t >/dev/null || { echo "need $t" >&2; exit 2; }; done
301
302# Quiet flag: wait for it to clear, never touch it.
303QUIET=$HOME/workspace/.spark-quiet
304if [ "$GB" = 1 ]; then
305 # v4-gb10 runs INSIDE a quietlock hold given by the coordinator (never taken or touched here).
306 [ -s "$QUIET" ] || { echo "v4-gb10 must run inside a quietlock hold (quiet flag not held)" >&2; exit 3; }
307else
308while [ -s "$QUIET" ]; do echo "quiet flag set; waiting"; sleep 30; done
309fi
310
311# The tree must be clean (apart from the outputs) so the receipt commit is the code that ran.
312DIRTY=$(git -C "$REPO" status --porcelain -- . ":!docs/campaigns/allen-e2e/receipts-$V.jsonl" ":!docs/campaigns/allen-e2e/artifacts-$V" ":!docs/campaigns/allen-e2e/RESULT-$V.md" | wc -l)
313if [ "$DIRTY" -ne 0 ] && [ -z "${DEMO_ALLOW_DIRTY:-}" ]; then
314 echo "working tree has $DIRTY uncommitted changes; commit them first" >&2; exit 2
315fi
316COMMIT=$(git -C "$REPO" rev-parse HEAD)
317# v2 change 3: name the stack. It is main only if every file this branch changes against
318# origin/main is a demo-only file (this driver, the demo docs, the test-only subject fixture, crumbs).
319git -C "$REPO" fetch -q origin main 2>/dev/null
320MAIN=$(git -C "$REPO" rev-parse origin/main)
321NONDEMO=$(git -C "$REPO" diff --name-only "$MAIN" HEAD | grep -vE "^(scripts/allen_e2e_demo\.sh|docs/campaigns/allen-e2e/|crates/aien-allen/tests/e2e_demo_subject\.rs$|(.*/)?\.crumb$)" | wc -l)
322if git -C "$REPO" merge-base --is-ancestor "$MAIN" HEAD && [ "$NONDEMO" -eq 0 ]; then
323 STACK="main at ${MAIN:0:12} plus demo-only files"
324else
325 STACK="candidate stack at ${COMMIT:0:12}, not main"
326fi
327
328if [ "$GB" = 1 ]; then
329 # The sealed qualified candidate (sc#277 attempt 3): sovereign-core 6bbe2ec2, built with the GPU lib.
330 # Refuse unless it is exactly the sealed file and this tree differs from 6bbe2ec2 only by demo files.
331 Q277=${DEMO_GB10_SEAL_DIR:-$HOME/workspace/hive/Q277}
332 GB_SHA_WANT=152c0aecce662f618bf683c8854d6de56a7075e0461c2433570f4c15b68571a5
333 GB_SRC=6bbe2ec269768c7c9b94b9484c757ca45f55f564
334 GBIN=$Q277/bin/aien-cli-main
335 GB_SEAL=$(awk '$2=="bin/aien-cli-main"{print $1}' "$Q277/SHA256SUMS" 2>/dev/null)
336 GB_SHA=$(sha256sum "$GBIN" 2>/dev/null | cut -d' ' -f1)
337 [ -n "$GB_SHA" ] && [ "$GB_SHA" = "$GB_SHA_WANT" ] && [ "$GB_SEAL" = "$GB_SHA_WANT" ] || { echo "sealed GB10 binary missing or sha256 mismatch ($GB_SHA vs $GB_SHA_WANT): refusing" >&2; exit 2; }
338 GB_NONDEMO=$(git -C "$REPO" diff --name-only "$GB_SRC" HEAD | grep -vE "^(scripts/allen_e2e_demo\.sh|docs/campaigns/allen-e2e/|crates/aien-allen/tests/e2e_demo_subject\.rs$|(.*/)?\.crumb$)" | wc -l)
339 git -C "$REPO" merge-base --is-ancestor "$GB_SRC" HEAD && [ "$GB_NONDEMO" -eq 0 ] || { echo "tree is not 6bbe2ec2 plus demo-only files: the sealed binary would not be this code; refusing" >&2; exit 2; }
340 for v in AIEN_OMEGA_SPIN_US AIEN_OMEGA_CTA_BUDGET AIEN_COMPOSE_BUDGET_MS; do
341 [ -z "$(printenv $v)" ] || { echo "$v must be unset for the GB10 candidate" >&2; exit 3; }
342 done
343 [ -z "$(nvidia-smi --query-compute-apps=pid --format=csv,noheader 2>/dev/null | tr -d ' \n')" ] || { echo "another GPU process is present; refusing" >&2; exit 3; }
344fi
345
346# Build with the real compose library (no omega dir, no GPU lib).
347(
348 cd "$REPO" && env -u AIEN_OMEGA_DIR -u AIEN_OMEGA_COMPOSE_DIR -u AIEN_OMEGA_GPU_LIB -u AIEN_FORCE_CPU_STUB \
349 AIEN_OMEGA_COMPOSE_LIB="$LIB" CARGO_TARGET_DIR="$D/target" cargo build --release -p aien-cli
350) >"$D/build-run.log" 2>&1 || { echo "build failed, see $D/build-run.log" >&2; exit 2; }
351grep -q "building the stub" "$D/build-run.log" && { echo "stub compose build; refusing" >&2; exit 2; }
352BIN_SHA=$(sha256sum "$BIN" | cut -d' ' -f1)
353
354# Fresh state. An older run directory is moved aside, never deleted.
355[ -e "$RUN" ] && mv "$RUN" "$RUN.old-$(date +%s)"
356mkdir -p "$RUN"/{state,compose,prov,ws,home,logs} "$ART"
357: >"$RECEIPTS"
358LOGS=$RUN/logs
359WS=$RUN/ws
360
361E=(env -u AIEN_OMEGA_DIR -u AIEN_OMEGA_COMPOSE_DIR -u AIEN_OMEGA_GPU_LIB -u AIEN_REQUIRE_BLACKWELL
362 -u AIEN_GPU_BACKEND -u AIEN_MODEL_DIR -u AIEN_MODEL_PATH -u AIEN_TOKENIZER_PATH -u AIEN_FORCE_CPU_STUB
363 -u AIEN_FAULT_HOLD -u AIEN_FAULT_HOLD_FILE -u AIEN_ALLEN_SUBJECT -u AIEN_ALLEN_ADOPT
364 -u AIEN_COMPOSE_MAX_TOKENS -u AIEN_COMPOSE_DOC_MAX_TOKENS
365 HOME="$RUN/home" NO_COLOR=1 AIEN_RUNTIME_SOCK="$SOCK" AIEN_RUNTIME_STATE_DIR="$RUN/state"
366 AIEN_COMPOSE_DIR="$RUN/compose" AIEN_PROVENANCE_DIR="$RUN/prov" AIEN_REQUIRE_CHECKPOINT=1
367 AIEN_COMPOSE_AUTHORIZE_REQUIRES_DESK=1 AIEN_COMPOSE_EDIT_BUDGET_MS=599000 AIEN_COMPOSE_DOC_BUDGET_MS=599000)
368cli() { "${E[@]}" "$BIN" "$@"; }
369
370# Weights digests. Single file: its sha256. Sharded: sha256 of the sorted
371# "<sha256> <name>" listing of every shard (the daemon itself digests only the
372# shard file it is pointed at).
373weights_digest() {
374 local d=$1
375 if [ -f "$d/model.safetensors" ]; then sha256sum "$d/model.safetensors" | cut -d' ' -f1
376 else (cd "$d" && sha256sum -- *.safetensors | LC_ALL=C sort -k2 | sha256sum | cut -d' ' -f1); fi
377}
378WD_A=$(weights_digest "$MA"); WD_B=$(weights_digest "$MB")
379
380# ---- receipts ----------------------------------------------------------------
381CUR_DIR=none; CUR_WD=none; CUR_BACKEND="no daemon started"; CUR_MODELLINE=none
382CUR_DSHA=none; CUR_TSHA=none; CUR_MAC="no daemon started"; ID0=none
383rec() { # step status evidence [extra json]
384 local extra=${4:-'{}'}
385 jq -nc --arg step "$1" --arg status "$2" --arg ev "$3" --argjson extra "$extra" \
386 --arg commit "$COMMIT" --arg bin "$BIN_SHA" --arg backend "$CUR_BACKEND" --arg modelline "$CUR_MODELLINE" \
387 --arg mdir "$CUR_DIR" --arg wd "$CUR_WD" --arg dsha "$CUR_DSHA" --arg tsha "$CUR_TSHA" \
388 --arg mac "$CUR_MAC" --arg id0 "$ID0" --arg stack "$STACK" --arg label "$LABEL" --arg ver "$V" --arg utc "$(date -u +%Y-%m-%dT%H:%M:%SZ)" \
389 '{step:$step,status:$status,label:$label,stack:$stack,spec:("DEMO-"+$ver),evidence:$ev,
390 repo_commit:$commit,daemon_sha256:$bin,cli_sha256:$bin,
391 backend_line:$backend,model_line:$modelline,model_dir:$mdir,
392 model_weights_digest:$wd,daemon_model_sha256:$dsha,tokenizer_sha256:$tsha,
393 authorize_mac_line:$mac,allen_logical_agent_id:$id0,utc:$utc} + $extra' >>"$RECEIPTS"
394 echo "[$(date -u +%H:%M:%S)] $1 $2: $3" | cut -c1-300
395}
396
397# ---- daemon control ----------------------------------------------------------
398DN=0; DPID=""; ABORT=""
399start_daemon() { # model_dir [VAR=val ...]; sets CUR_* from the daemon log
400 local d=$1; shift
401 local mp="$d/model.safetensors"; [ -f "$mp" ] || mp="$d/model-00001-of-00003.safetensors"
402 local dbin=$BIN; IS_GB=0
403 if [ "$GB" = 1 ] && [ "$d" = "$MA" ]; then
404 # a GB10 life: the sealed Q277 binary and its qualified env; never start another life after an unclean stop
405 [ "$GB_UNCLEAN" = 0 ] || { echo "previous GB10 stop was not clean; not starting another GB10 daemon"; return 1; }
406 dbin=$GBIN; IS_GB=1; mp="$d/model.safetensors.index.json"
407 set -- AIEN_GB10_QWEN3_DECLARED_ATTEMPT=1 AIEN_REQUIRE_BLACKWELL=1 AIEN_KV_CONTEXT_TOKENS=4096 "$@"
408 NVRM_BEFORE=$(nvrm_count)
409 fi
410 DN=$((DN + 1)); DLOG=$LOGS/daemon-$DN.log; rm -f "$SOCK"
411 if [ "$IS_GB" = 1 ]; then
412 gb_spawn "$DLOG" "${E[@]}" AIEN_MODEL_PATH="$mp" AIEN_TOKENIZER_PATH="$d/tokenizer.json" "$@" "$dbin" daemon
413 else
414 "${E[@]}" AIEN_MODEL_PATH="$mp" AIEN_TOKENIZER_PATH="$d/tokenizer.json" "$@" "$dbin" daemon >"$DLOG" 2>&1 &
415 DPID=$!
416 fi
417 DAEMON_GB=$IS_GB
418 if [ "$IS_GB" = 1 ]; then
419 # the GB10 daemon must be the leader of its own session (so group and terminal signals cannot reach it)
420 local k sid=""
421 for k in $(seq 1 50); do sid=$(sed 's/.*) //' "/proc/$DPID/stat" 2>/dev/null | awk '{print $4}'); [ "$sid" = "$DPID" ] && break; sleep 0.1; done
422 [ "$sid" = "$DPID" ] || { echo "GB10 daemon pid $DPID is not its own session leader (session '$sid'); stopping it gracefully"; gb_stop; return 1; }
423 fi
424 local i
425 for i in $(seq 1 600); do
426 sleep 2
427 alive "$DPID" || { [ "$IS_GB" = 1 ] && gb_early_exit; DPID=""; DAEMON_GB=0; echo "daemon exited early: $(tail -c 400 "$DLOG")"; return 1; }
428 if [ -S "$SOCK" ] && grep -q "Replay reconcile:" "$DLOG"; then break; fi
429 done
430 [ -S "$SOCK" ] || { echo "daemon not serving after 20 min"; return 1; }
431 CUR_DIR=$d
432 if [ "$d" = "$MA" ]; then CUR_WD=$WD_A; else CUR_WD=$WD_B; fi
433 CUR_BACKEND=$(grep -m1 '^ Backend:' "$DLOG" | sed 's/^ *//')
434 CUR_MODELLINE=$(grep -m1 '^ Model:' "$DLOG" | sed 's/^ *//')
435 CUR_MAC=$(grep -m1 '^Authorize MAC:' "$DLOG")
436 CUR_DSHA=$(sed -n 's/.*model_sha256=\([0-9a-f]\{64\}\).*/\1/p' "$DLOG" | head -1)
437 CUR_TSHA=$(sed -n 's/.*tokenizer_sha256=\([0-9a-f]\{64\}\).*/\1/p' "$DLOG" | head -1)
438 : "${CUR_DSHA:=none}" "${CUR_TSHA:=none}"
439 if [ "$IS_GB" = 1 ]; then
440 # v4-gb10: the GB10 Backend line and the reservation line are required; else a graceful stop (never a kill).
441 CUR_RESV=$(grep -a -m1 'GB10_SERVING_RESERVATION reserved bytes=' "$DLOG" | sed 's/^ *//' | cut -c1-300)
442 case $CUR_BACKEND in *OmegaGb10Backend*) ;; *) echo "not the GB10 backend: $CUR_BACKEND"; gb_stop; return 1 ;; esac
443 [ -n "$CUR_RESV" ] || { echo "no GB10 reservation line"; gb_stop; return 1; }
444 return 0
445 fi
446 case $CUR_BACKEND in *CPU-reference*) ;; *) echo "not the CPU reference backend: $CUR_BACKEND"; safe_kill9 "$DPID"; wait "$DPID" 2>/dev/null; DPID=""; return 1 ;; esac
447 return 0
448}
449KILL_RC=""
450kill9() {
451 GB_STOP_DESC=""
452 # v4-gb10: a live GB10 daemon is never signalled; stop it through its shutdown path and wait without a timeout.
453 if [ "$GB" = 1 ] && [ -n "$GB_LIVE_PID" ]; then gb_stop; local r=$?; KILL_RC=$GB_EXIT; return $r; fi
454 [ -n "$DPID" ] || { KILL_RC="no daemon"; return 1; }
455 safe_kill9 "$DPID"; wait "$DPID" 2>/dev/null; KILL_RC=$?; DPID=""; rm -f "$SOCK"
456 [ "$KILL_RC" -eq 137 ]
457}
458if [ "$GB" = 1 ]; then
459 # exit path: a GB10 daemon still alive is stopped gracefully (unbounded wait), a CPU daemon is killed as in v1-v3
460 gb_exit() { if [ -n "$GB_LIVE_PID" ]; then gb_stop; elif [ -n "$DPID" ]; then kill9; fi; }
461 trap gb_exit EXIT
462 gb_install_traps
463else
464trap '[ -n "$DPID" ] && kill -9 "$DPID" 2>/dev/null' EXIT
465fi
466
467# ---- snapshots used by S5-S7 -------------------------------------------------
468snap() { # prefix: profile, memory inspect, goals, effects ledger, document
469 local p=$1
470 cli allen show | jq -S .result >"$ART/$p-profile.json"
471 cli allen memory inspect --owner 1 | jq -S .result.result >"$ART/$p-memory.json"
472 cli allen goals list --owner 1 | jq -S .result.result >"$ART/$p-goals.json"
473 cli compose effects | jq -S .ledger >"$ART/$p-ledger.json"
474 # Every daemon open adds 5 bookkeeping records of kind 65538 (same kind as record 1, no effect, no
475 # authorization). So compare the host (effect-class) records, not the raw total.
476 cli compose recall | jq -S ".recall.host|length" >"$ART/$p-records.json"
477 cli compose recall | jq -S .recall.records_total >"$ART/$p-records-total.json"
478}
479same() { cmp -s "$ART/$1-$3.json" "$ART/$2-$3.json"; }
480
481sha_of() { sha256sum "$1" | cut -d' ' -f1; }
482skip() { [ -n "$ABORT" ] && { rec "$1" NOT_RUN "$ABORT"; return 0; }; return 1; }
483
484# One model task: propose -> authorize (desk MAC) -> execute -> checker. $1 tag, $2 file, $3 goal
485model_task() {
486 local tag=$1 file=$2 goal=$3
487 TASK_STATUS=FAIL; TASK_EV=""; TASK_COMMIT=0; TASK_DONE=0; TASK_JSON='{}'
488 cli compose propose --goal "$goal" --workspace "$WS" --context work >"$ART/$tag-propose.json" 2>"$ART/$tag-propose.err"
489 local rc=$?
490 if [ "$(jq -r '.report.committed // false' "$ART/$tag-propose.json" 2>/dev/null)" != true ]; then
491 TASK_EV="propose did not commit (exit $rc): $(jq -r '.report.proposer_error // .error // "no proposal"' "$ART/$tag-propose.json" 2>/dev/null | cut -c1-200); model attempts recorded verbatim in $tag-propose.json. No retry (spec)."
492 return
493 fi
494 TASK_COMMIT=1
495 local mem_ctx mem_n
496 mem_ctx=$(jq -r '.report.memory.context' "$ART/$tag-propose.json"); mem_n=$(jq -r '.report.memory.items_included' "$ART/$tag-propose.json")
497 cli compose authorize --report "$ART/$tag-propose.json" --workspace "$WS" --approver drake --desk 1 >"$ART/$tag-authorize.json" 2>"$ART/$tag-authorize.err"
498 if [ "$(jq -r '.ok' "$ART/$tag-authorize.json" 2>/dev/null)" != true ]; then
499 TASK_EV="authorize refused: $(jq -c . "$ART/$tag-authorize.json" | cut -c1-250)"; return
500 fi
501 local grant; grant=$(jq -r '.authorization.id' "$ART/$tag-authorize.json")
502 cli compose execute --report "$ART/$tag-propose.json" --workspace "$WS" --authorization "$grant" >"$ART/$tag-execute.json" 2>"$ART/$tag-execute.err"
503 local st; st=$(jq -r '.state // "none"' "$ART/$tag-execute.json")
504 cli compose effects | jq -S . >"$ART/$tag-effects.json"
505 cli compose recall >"$ART/$tag-recall.json"
506 local failed; failed=$(check_doc "$WS/$file")
507 [ -f "$WS/$file" ] && cp "$WS/$file" "$ART/$file"
508 TASK_JSON=$(jq -nc --arg f "$file" --arg failed "$failed" --arg st "$st" --arg mc "$mem_ctx" --argjson mn "${mem_n:-0}" --argjson grant "$grant" \
509 '{file:$f,constraints_failed:$failed,effect_state:$st,memory_context:$mc,items_included:$mn,grant_id:$grant}')
510 [ "$st" = DONE ] && TASK_DONE=1
511 if [ "$st" != DONE ]; then
512 case $st in UNRESOLVED|UNCERTAIN) TASK_STATUS=UNRESOLVED ;; esac
513 TASK_EV="effect state $st (not DONE); constraints failed: [${failed:-none}]"; return
514 fi
515 if [ "$mem_ctx" != work ] || [ "${mem_n:-0}" -lt 1 ]; then
516 TASK_EV="MemoryReport context=$mem_ctx items_included=$mem_n (need work, >=1); constraints failed: [${failed:-none}]"; return
517 fi
518 if [ -n "$failed" ]; then
519 TASK_EV="commit DONE, MemoryReport work/$mem_n, but document fails: $failed (document in artifacts-$V/$file, recorded verbatim)"; return
520 fi
521 TASK_STATUS=PASS
522 TASK_EV="commit DONE, MemoryReport context=work items_included=$mem_n, C1-C5 hold, $(wc -w <"$WS/$file") words, sha256 $(sha_of "$WS/$file")"
523}
524
525# The link chain itself (v2): the commit, grant, intent and ack records of this task each name
526# the same generation record and ALLEN agent, and that generation record's model_sha256 is the
527# digest the daemon loaded. Read through recall, digest-verified. Sets LINK_MODEL, LINK_ID (0/1)
528# and LINK_JSON. Stricter than the v1 text search, which only asked whether the values appear.
529record_text() { cli compose recall --ids "$1" | jq -c 'if .recall.cited[0].verified == true then (.recall.cited[0].text|fromjson) else {unverified:true} end' 2>/dev/null || echo '{"unreadable":true}'; }
530follow_link() { # tag file msha
531 local tag=$1 file=$2 msha=$3 gen commit grant intent ack id r ok_m=1 ok_i=1 rows="[]"
532 gen=$(jq -r '.report.generation_record // "none"' "$ART/$tag-propose.json")
533 commit=$(jq -r '.report.compose_commit // "none"' "$ART/$tag-propose.json")
534 grant=$(jq -r '.authorization.id // "none"' "$ART/$tag-authorize.json")
535 intent=$(jq -r --arg f "$file" '[.ledger.intents[]|select(.path==$f)][0].intent // "none"' "$ART/$tag-effects.json")
536 ack=$(jq -r --arg f "$file" '[.ledger.intents[]|select(.path==$f)][0].state_record // "none"' "$ART/$tag-effects.json")
537 for id in "$commit" "$grant" "$intent" "$ack"; do
538 case $id in ''|none|null) ok_m=0; ok_i=0; continue ;; esac
539 r=$(record_text "$id")
540 [ "$(printf '%s' "$r" | jq -r '.provenance.generation_record|tostring')" = "$gen" ] || ok_m=0
541 [ "$(printf '%s' "$r" | jq -r '.provenance.allen_agent // "absent"')" = "$ID0" ] || ok_i=0
542 rows=$(printf '%s' "$rows" | jq -c --argjson id "$id" --argjson r "$r" '. + [{record:$id,provenance:($r.provenance // null)}]')
543 done
544 local g gm="none"
545 case $gen in ''|none|null) ok_m=0 ;; *) g=$(record_text "$gen"); gm=$(printf '%s' "$g" | jq -r '.model_sha256 // "none"'); [ "$gm" = "$msha" ] || ok_m=0 ;; esac
546 LINK_MODEL=$ok_m; LINK_ID=$ok_i
547 LINK_JSON=$(jq -nc --arg gen "$gen" --arg gm "$gm" --argjson rows "$rows" --arg c "$commit" --arg g "$grant" --arg i "$intent" --arg a "$ack" \
548 '{generation_record:$gen,generation_record_model_sha256:$gm,chain:{compose_commit:$c,grant:$g,intent:$i,ack:$a},records:$rows}')
549}
550
551# Provenance links for one commit: which of the five items appear in what the daemon recorded.
552# Sources: the effect ledger, every compose record text (recall), the task report, the authorize
553# and execute answers, and the provenance receipts. NOT the daemon boot log (it is not a ledger).
554provenance() { # tag file model_sha_expected
555 local tag=$1 file=$2 msha=$3 ledger recall psha csha auth pool
556 recall=$ART/$tag-recall.json; ledger=$ART/$tag-effects.json
557 psha=$(jq -r '.report.proposal_sha256' "$ART/$tag-propose.json")
558 csha=$(sha_of "$WS/$file")
559 auth=$(jq -r '.authorization.id' "$ART/$tag-authorize.json")
560 local l_grant l_prop l_content l_model l_id
561 l_grant=$(jq -r --arg f "$file" --argjson a "$auth" '[.ledger.intents[]|select(.path==$f and .authorization==$a)]|length' "$ledger")
562 l_content=$(jq -r --arg f "$file" --arg c "$csha" '[.ledger.intents[]|select(.path==$f and .content_sha256==$c)]|length' "$ledger")
563 pool=$(cat "$ledger" "$recall" "$ART/$tag-propose.json" "$ART/$tag-authorize.json" "$ART/$tag-execute.json" "$RUN"/prov/*.json 2>/dev/null)
564 l_prop=$(printf '%s' "$pool" | grep -c "$psha")
565 l_model=$(printf '%s' "$pool" | grep -c "$msha")
566 l_id=$(printf '%s' "$pool" | grep -c "$ID0")
567 LINK_JSON=null
568 if [ "$V" != v1 ]; then
569 follow_link "$tag" "$file" "$msha"
570 l_model=$LINK_MODEL; l_id=$LINK_ID
571 fi
572 PROV_JSON=$(jq -nc --argjson g "${l_grant:-0}" --argjson p "$l_prop" --argjson c "${l_content:-0}" --argjson m "$l_model" --argjson i "$l_id" \
573 --arg auth "$auth" --arg psha "$psha" --arg csha "$csha" --arg msha "$msha" --argjson link "$LINK_JSON" \
574 '{grant_id:$auth,proposal_sha256:$psha,content_sha256:$csha,model_digest_expected:$msha,link_chain:$link,
575 ledger_links:{grant_id:($g>0),proposal_sha256:($p>0),content_sha256_equals_disk:($c>0),model_digest:($m>0),logical_agent_id:($i>0)}}')
576 [ "${l_grant:-0}" -gt 0 ] && [ "$l_prop" -gt 0 ] && [ "${l_content:-0}" -gt 0 ] && [ "$l_model" -gt 0 ] && [ "$l_id" -gt 0 ]
577}
578
579# ============================== S0 ============================================
580cli compose desk-key --create 1 >"$ART/S0-deskkey.json" 2>&1 # prints id and path, never the key
581if start_daemon "$MA"; then
582 ST_ALLEN=$(cli allen status | jq -r .result.identity)
583 ALLEN_DIRS=$(ls -d "$RUN"/compose.allen-* "$RUN"/compose/*allen* 2>/dev/null | wc -l)
584 if [[ $CUR_MAC == "Authorize MAC: on"* ]] && [ "$ST_ALLEN" = not_engaged ] && [ "$ALLEN_DIRS" -eq 0 ]; then
585 rec S0 PASS "daemon up on M-A ($MABK), log: '$CUR_MAC', allen status identity=$ST_ALLEN, 0 ALLEN state directories before S1" "$(if [ "$GB" = 1 ]; then jq -nc --arg r "${CUR_RESV:-}" '{gb10_reservation_line:$r}'; else echo '{}'; fi)"
586 else
587 rec S0 FAIL "MAC line='$CUR_MAC' identity=$ST_ALLEN allen_dirs=$ALLEN_DIRS"
588 ABORT="S0 failed, nothing further can be trusted"
589 fi
590else
591 rec S0 FAIL "daemon did not start on M-A: $(tail -c 300 "$LOGS/daemon-$DN.log" 2>/dev/null | tr '\n' ' ')"
592 ABORT="S0 failed: daemon did not start on M-A"
593fi
594
595# ============================== S1 ============================================
596if ! skip S1; then
597 # The compose home exists once the daemon opened it; its record 1 is the lineage a subject must bind.
598 LINEAGE=$(cli compose recall --ids 1 | jq -r '.recall.cited[0].digest')
599 kill9 || true
600 SUBJ=$RUN/subject.bin
601 FX=$(cd "$REPO" && env -u AIEN_OMEGA_DIR -u AIEN_OMEGA_COMPOSE_DIR AIEN_OMEGA_COMPOSE_LIB="$LIB" CARGO_TARGET_DIR="$D/target" \
602 DEMO_SUBJECT_OUT="$SUBJ" DEMO_LINEAGE_HEX="$LINEAGE" DEMO_SUBJECT_NAME="allen-e2e-demo-$V" \
603 cargo test --release -p aien-allen --test e2e_demo_subject -- --ignored --nocapture 2>&1)
604 AGENT=$(printf '%s\n' "$FX" | sed -n 's/^AGENT=//p'); AROOT=$(printf '%s\n' "$FX" | sed -n 's/^ROOT=//p')
605 if [ -z "$AGENT" ] || [ ! -s "$SUBJ" ]; then
606 rec S1 FAIL "could not build the subject fixture: $(printf '%s' "$FX" | tail -c 300)"
607 ABORT="S1 failed: no identity"
608 elif start_daemon "$MA" AIEN_ALLEN_SUBJECT="$SUBJ" AIEN_ALLEN_ADOPT="$AGENT"; then
609 ALOG=$(grep '^ALLEN: engaged' "$LOGS/daemon-$DN.log")
610 STJ=$(cli allen status | jq -c .)
611 FP=$(printf '%s' "$STJ" | jq -r .result.fingerprint)
612 LOGID=$(printf '%s' "$ALOG" | sed -n 's/.*agent=\([0-9a-f]\{64\}\).*/\1/p')
613 NLINES=$(printf '%s\n' "$ALOG" | grep -c .)
614 if [ "$(printf '%s' "$STJ" | jq -r .result.identity)" = engaged ] && [ "$LOGID" = "$AGENT" ] && [ "$FP" = "${AGENT:0:8}" ] && [ "$NLINES" -eq 1 ]; then
615 ID0=$AGENT
616 rec S1 PASS "ID0=$ID0 (daemon log 'ALLEN: engaged agent=...', allen status fingerprint $FP); AgentRoot $AROOT; one identity" \
617 "$(jq -nc --arg root "$AROOT" --arg lineage "$LINEAGE" '{agent_root:$root,cortex_lineage_record1:$lineage,subject_kind:"host-built fixture (test-only encoder), adopted once by AIEN_ALLEN_ADOPT; real subjects come only from AIENOS cs_provision (NOT_RUN)"}')"
618 else
619 rec S1 FAIL "status/log disagree: identity=$(printf '%s' "$STJ" | jq -r .result.identity) fp=$FP log=$LOGID lines=$NLINES"
620 ABORT="S1 failed: identity not engaged"
621 fi
622 else
623 rec S1 FAIL "daemon refused or did not start with the subject: $(tail -c 300 "$LOGS/daemon-$DN.log" | tr '\n' ' ')"
624 ABORT="S1 failed: identity not engaged"
625 fi
626fi
627
628# ============================== S2 ============================================
629if ! skip S2; then
630 cli allen set --expect 0 --plain-language 1 >"$ART/S2-set.json"
631 cli allen memory put --context work --text "$NWORK" >"$ART/S2-put-work.json"
632 cli allen memory put --context personal --text "$NPERS" >"$ART/S2-put-pers.json"
633 cli allen goals add --context work --text "$G1" >"$ART/S2-goal.json"
634 snap S2
635 OK_PROF=$(jq -r --arg id "$ID0" '.profile.persona.plain_language==true and .profile.agent==$id and .revision==1' "$ART/S2-profile.json")
636 OK_MEM=$(jq -r --arg a "$NWORK" --arg b "$NPERS" --arg g "$G1" \
637 '(map(select(.state=="live"))|length)==3 and (map(select(.scope=="work" and .kind=="fact" and .text==$a))|length)==1 and (map(select(.scope=="personal" and .kind=="fact" and .text==$b))|length)==1 and (map(select(.scope=="work" and .kind=="goal" and .text==$g))|length)==1' "$ART/S2-memory.json")
638 OK_GOALS=$(jq -r --arg g "$G1" '(.goals|length)==1 and .goals[0].text==$g and .goals[0].scope=="work" and .goals[0].state=="open"' "$ART/S2-goals.json")
639 if [ "$OK_PROF" = true ] && [ "$OK_MEM" = true ] && [ "$OK_GOALS" = true ]; then
640 rec S2 PASS "profile revision 1 plain-language=1 under ID0; memory inspect shows exactly N-work (work), N-pers (personal), G1 (work goal); goals list shows exactly G1"
641 else
642 rec S2 FAIL "profile_ok=$OK_PROF memory_ok=$OK_MEM goals_ok=$OK_GOALS (see artifacts S2-*.json)"
643 fi
644 NPERS_ITEM=$(jq -r --arg b "$NPERS" '.[]|select(.text==$b)|.item' "$ART/S2-memory.json")
645fi
646
647# ============================== S3 ============================================
648S3_OK=0
649if ! skip S3; then
650 model_task S3 garden.md "$GOAL_A"
651 rec S3 "$TASK_STATUS" "$TASK_EV" "$TASK_JSON"
652 S3_OK=$TASK_DONE
653fi
654
655# ============================== S3-red ========================================
656if ! skip S3-red; then
657 printf '# Garden plan\n\nPlant carrots and beans along the south fence.\nNote: %s\n' "$CANARY" >"$ART/red-garden.md"
658 RED=$(check_doc "$ART/red-garden.md")
659 printf "# Garden plan\n\nPlant tomatoes in May.\n" >"$ART/good-control.md"; GOOD=$(check_doc "$ART/good-control.md")
660 if [ "$RED" = "C3 C5" ]; then
661 rec S3-red PASS "checker on the hand-written document (breaks C3 and C5) reports exactly: $RED; positive control (good document) reports: [${GOOD:-none}]" "$(jq -nc --arg r "$RED" '{checker_failed:$r}')"
662 else
663 rec S3-red FAIL "checker reported '$RED', expected exactly 'C3 C5'" "$(jq -nc --arg r "$RED" '{checker_failed:$r}')"
664 fi
665fi
666
667# ============================== S4 ============================================
668if ! skip S4; then
669 if [ "$S3_OK" != 1 ]; then
670 rec S4 NOT_RUN "S3 produced no commit, so there is no commit to trace"
671 elif provenance S3 garden.md "$CUR_DSHA"; then
672 rec S4 PASS "ledger links grant id, proposal sha256, content sha256 (= garden.md on disk), M-A model digest and ID0" "$PROV_JSON"
673 else
674 MISS=$(printf '%s' "$PROV_JSON" | jq -r '.ledger_links|to_entries|map(select(.value==false).key)|join(",")')
675 rec S4 FAIL "missing links in what the daemon recorded for the S3 commit: $MISS. Present: the rest$([ "$V" = v1 ] && echo ". The model digest and ID0 appear in the daemon boot log and the ALLEN profile/memory stores, not in the compose or effect ledger records of the commit")" "$PROV_JSON"
676 fi
677fi
678
679# ============================== S5 ============================================
680if ! skip S5; then
681 snap S5a
682 [ -f "$WS/garden.md" ] && { G_SHA_BEFORE=$(sha_of "$WS/garden.md"); G_STAT_BEFORE=$(stat -c '%i %Y.%y' "$WS/garden.md"); } || { G_SHA_BEFORE=none; G_STAT_BEFORE=none; }
683 if kill9; then KILLED="kill -9 (exit 137)"; else KILLED="kill rc=$KILL_RC"; fi
684 [ -n "$GB_STOP_DESC" ] && KILLED=$GB_STOP_DESC
685 if start_daemon "$MA" AIEN_ALLEN_SUBJECT="$RUN/subject.bin"; then
686 snap S5b
687 G_SHA_AFTER=none; G_STAT_AFTER=none
688 [ -f "$WS/garden.md" ] && { G_SHA_AFTER=$(sha_of "$WS/garden.md"); G_STAT_AFTER=$(stat -c '%i %Y.%y' "$WS/garden.md"); }
689 REPL=$(grep -m1 '^Replay reconcile:' "$LOGS/daemon-$DN.log")
690 FPA=$(cli allen status | jq -r .result.fingerprint)
691 ALOG2=$(grep -c "^ALLEN: engaged agent=$ID0" "$LOGS/daemon-$DN.log")
692 CHK=""
693 [ "$FPA" = "${ID0:0:8}" ] && [ "$ALOG2" = 1 ] || CHK="$CHK identity"
694 same S5a S5b profile || CHK="$CHK profile"
695 same S5a S5b memory || CHK="$CHK notes"
696 same S5a S5b goals || CHK="$CHK goals"
697 same S5a S5b ledger || CHK="$CHK ledger-changed(re-executed?)"
698 same S5a S5b records || CHK="$CHK host-record-count"
699 [ "$S3_OK" = 1 ] || CHK="$CHK no-S3-commit"
700 [ "$(jq -r '[.intents[]|select(.path=="garden.md" and .state=="DONE")]|length' "$ART/S5b-ledger.json")" = 1 ] || CHK="$CHK S3-not-DONE"
701 [ "$G_SHA_BEFORE" != none ] && [ "$G_SHA_BEFORE" = "$G_SHA_AFTER" ] && [ "$G_STAT_BEFORE" = "$G_STAT_AFTER" ] || CHK="$CHK garden.md"
702 if [ -z "$CHK" ]; then
703 rec S5 PASS "$KILLED then restart on $MAL: ID0 same, profile/notes/G1 identical, S3 intent still DONE and ledger+record count unchanged (not re-executed), garden.md same sha256, inode and mtime; $REPL$HCNOTE"
704 elif [ "$S3_OK" != 1 ]; then
705 rec S5 NOT_RUN "no S3 commit existed to check; restart itself: identity/profile/notes/goals problems=[${CHK# }]"
706 else
707 rec S5 FAIL "differences after restart:${CHK}; $REPL"
708 fi
709 else
710 rec S5 FAIL "$KILLED; daemon did not restart on M-A: $(tail -c 300 "$LOGS/daemon-$DN.log" | tr '\n' ' ')"
711 ABORT="S5: daemon would not restart"
712 fi
713fi
714
715# ============================== S6 ============================================
716if ! skip S6; then
717 cli allen memory forget --context personal --item "${NPERS_ITEM:-none}" >"$ART/S6-forget.json"
718 snap S6
719 cli allen memory recall --context personal >"$ART/S6-recall-personal.json"
720 LIVE=$(jq -r --arg b "$NPERS" '[.[]|select(.text==$b or .state=="live" and .scope=="personal")]|length' "$ART/S6-memory.json")
721 SHOWN=$(jq -r --arg i "${NPERS_ITEM:-none}" '[.[]|select(.item==$i)|.state]|join(",")' "$ART/S6-memory.json")
722 RITEMS=$(jq -r '.result.result.items|length' "$ART/S6-recall-personal.json")
723 DISK=$(grep -rl "$NPERS" "$RUN/compose" "$RUN/compose.allen-memory" "$RUN/compose.allen-profile" "$RUN/state" "$RUN/prov" "$LOGS" 2>/dev/null | wc -l)
724 if [ "$LIVE" -eq 0 ] && [ "$RITEMS" -eq 0 ] && [ "$(jq -r '.ok' "$ART/S6-forget.json")" = true ]; then
725 rec S6 PASS "forgot N-pers: inspect no longer shows its text (its row remains as state '${SHOWN:-absent}' with no text), recall --context personal returns 0 items; files under daemon state still containing the canary text: $DISK" \
726 "$(jq -nc --argjson d "$DISK" '{canary_files_remaining_in_daemon_state:$d}')"
727 else
728 rec S6 FAIL "after forget: live/visible=$LIVE recall_items=$RITEMS forget_ok=$(jq -r '.ok' "$ART/S6-forget.json")"
729 fi
730fi
731
732# ============================== S7 ============================================
733[ "$GB" = 1 ] && LABEL=real-CPU # v4-gb10: S0-S6 real-GB10, S7-S8 real-CPU
734if ! skip S7; then
735 OLD_BACKEND=$CUR_BACKEND; OLD_MODELLINE=$CUR_MODELLINE; OLD_DSHA=$CUR_DSHA; OLD_TSHA=$CUR_TSHA
736 if kill9; then KILLED="kill -9 (exit 137)"; else KILLED="kill rc=$KILL_RC"; fi
737 [ -n "$GB_STOP_DESC" ] && KILLED=$GB_STOP_DESC
738 if start_daemon "$MB" AIEN_ALLEN_SUBJECT="$RUN/subject.bin"; then
739 snap S7
740 FPB=$(cli allen status | jq -r .result.fingerprint)
741 CHK=""
742 if [ "$V" != v1 ]; then
743 # v2 change 2: the Model: line must change; the Backend: line is recorded, not required to change.
744 [ "$CUR_MODELLINE" != "$OLD_MODELLINE" ] && [ "$CUR_MODELLINE" != none ] || CHK="$CHK model-line-identical"
745 else
746 [ "$CUR_BACKEND" != "$OLD_BACKEND" ] || CHK="$CHK backend-line-identical"
747 fi
748 # v4-gb10: model AND backend change (GB10 -> CPU reference); both lines must differ.
749 if [ "$GB" = 1 ]; then [ "$CUR_BACKEND" != "$OLD_BACKEND" ] || CHK="$CHK backend-line-identical"; fi
750 [ "$CUR_DSHA" != "$OLD_DSHA" ] && [ "$CUR_DSHA" != none ] || CHK="$CHK model-digest-not-changed"
751 [ "$FPB" = "${ID0:0:8}" ] || CHK="$CHK identity"
752 same S6 S7 profile || CHK="$CHK profile"
753 same S6 S7 memory || CHK="$CHK notes"
754 same S6 S7 goals || CHK="$CHK goals"
755 cli allen memory recall --context personal >"$ART/S7-recall-personal.json"
756 [ "$(jq -r '.result.result.items|length' "$ART/S7-recall-personal.json")" = 0 ] || CHK="$CHK N-pers-visible"
757 EXTRA=$(jq -nc --arg ob "$OLD_BACKEND" --arg nb "$CUR_BACKEND" --arg om "$OLD_MODELLINE" --arg nm "$CUR_MODELLINE" --arg od "$OLD_DSHA" --arg nd "$CUR_DSHA" \
758 '{before:{backend_line:$ob,model_line:$om,daemon_model_sha256:$od},after:{backend_line:$nb,model_line:$nm,daemon_model_sha256:$nd}}')
759 if [ -z "$CHK" ]; then
760 rec S7 PASS "$KILLED then start on M-B: $([ "$V" != v1 ] && echo "Model: line$([ "$GB" = 1 ] && echo " and Backend: line")" || echo "backend line") and model digest changed (${OLD_DSHA:0:12} -> ${CUR_DSHA:0:12}); ID0 same; profile, N-work, G1 unchanged; N-pers still absent" "$EXTRA"
761 else
762 rec S7 FAIL "criteria not met:${CHK}. Backend line before: '$OLD_BACKEND'; after: '$CUR_BACKEND'. Model line and digest changed: $([ "$CUR_DSHA" != "$OLD_DSHA" ] && echo yes || echo no)" "$EXTRA"
763 fi
764 else
765 rec S7 FAIL "$KILLED; daemon did not start on M-B: $(tail -c 400 "$LOGS/daemon-$DN.log" | tr '\n' ' ')"
766 ABORT="S7: M-B daemon did not start"
767 fi
768fi
769
770# ============================== S8 ============================================
771if ! skip S8; then
772 model_task S8 garden-b.md "$GOAL_B"
773 S8_STATUS=$TASK_STATUS; S8_EV=$TASK_EV
774 if [ "$TASK_COMMIT" = 1 ] && [ -f "$WS/garden-b.md" ]; then
775 if provenance S8 garden-b.md "$CUR_DSHA"; then
776 PROV_NOTE="provenance links all present (incl. M-B digest and ID0)"
777 else
778 MISS=$(printf '%s' "$PROV_JSON" | jq -r '.ledger_links|to_entries|map(select(.value==false).key)|join(",")')
779 PROV_NOTE="provenance links missing: $MISS (M-B digest expected in the provenance)"
780 [ "$S8_STATUS" = PASS ] && S8_STATUS=FAIL
781 fi
782 S8_EV="$S8_EV; $PROV_NOTE"
783 TASK_JSON=$(printf '%s' "$TASK_JSON" | jq -c --argjson p "$PROV_JSON" '. + {provenance:$p}')
784 fi
785 rec S8 "$S8_STATUS" "$S8_EV" "$TASK_JSON"
786fi
787
788[ -n "$DPID" ] && kill9
789if [ "$GB" = 1 ]; then
790 # One extra receipt for the GB10 daemon lives: the per-life PASS rule of DEMO-v4-gb10.md.
791 LABEL=real-GB10
792 GB_VERDICT=$(printf '%s' "$GB_LIVES" | jq -r 'if length==0 then "FAIL"
793 elif all(.[]; .backend_gb10 and .reservation_line_present and .alloc_failure_lines==0 and .rm_failure_lines==0 and .fallback_lines==0 and .nvrm_log_lines==0 and .seat_kill_lines==0 and .stop_clean) | not then "FAIL"
794 elif any(.[]; .nvrm_kernel_delta=="unavailable") then "UNKNOWN"
795 elif all(.[]; .nvrm_kernel_delta==0) then "PASS" else "FAIL" end')
796 rec GB10-lives "$GB_VERDICT" "per GB10 daemon life: GB10 Backend line, reservation line, 0 allocation/RM failures, 0 fallback lines, 0 NVRM log lines (daemon log and kernel log delta), no seat-kill line, graceful stop (exit 0, shutdown acked, socket removed). Sealed binary sha256 $GB_SHA. UNKNOWN means the kernel log was unreadable. hard-crash recovery on GB10: NOT TESTED (chip safety rule)" \
797 "$(jq -nc --argjson l "$GB_LIVES" --arg sha "$GB_SHA" --arg src "$GB_SRC" '{gb10_lives:$l,gb10_binary_sha256:$sha,gb10_binary_source_commit:$src,hard_crash_recovery_on_gb10:"NOT TESTED (chip safety rule)"}')"
798fi
799cp "$LOGS"/daemon-*.log "$ART/" 2>/dev/null
800echo "receipts: $RECEIPTS"