interplane.Get Omega
Read in your language

omega-mv

Omega: a language for verified programs, semantic identity and machine realization. Seed-born work and existing implementations have distinct provenance.

Brought by drakestapleton

branch main Branches Tags History

omega-mv / tests / runtime / rx_operator_host.sh

577 lines · 33005 bytes Raw Blame History
1#!/bin/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# R16 G6 operator control by execution (docs/r16-operator-control.md ยง7).
18#
19# rx_operator_host.sh <host|silicon> <production R13 binary> <rx_operator client>
20#
21# Starts the PRODUCTION R13 program (built without AIEN_TEST_BUILD) and drives
22# it only from outside: the rx_operator client over the owner-only socket, and
23# signals. Every check reads a reply, a file the world wrote in its state
24# directory, the program's exit status or the program's crumb-log audit line.
25# A case that cannot run here prints SKIPPED with its reason and the gate line
26# is NOT_RUN, never a pass. Silicon: the program is never killed (no SIGKILL
27# phase; on a failed check the world is resumed and the program is waited for).
28#
29# Host only, deterministic holds: the first program start of the control phase
30# runs under gdb in non-stop mode (only the thread at a breakpoint pauses; the
31# operator socket keeps serving). Three temporary breakpoints hold one point
32# each until the test has sent its request, then the test resumes the thread:
33# 1 rx_resident_accept the seat acceptor holding a computed seat result
34# (an activation in flight): the stop must cancel it
35# 2 rx_world_verify_crumbs the positive episode's work is done, its world is
36# not yet torn down: a stop here must keep it up
37# 3 rx_operator_open (2nd) the next world's open, after the positive world's
38# entry point closed: what close removed is visible
39# Without gdb those checks are SKIPPED (gate NOT_RUN). Silicon runs without
40# gdb: its in-flight count is reported, not checked.
41#
42# Environment: RX_OP_PHASES (default "startup control restart kill") runs a
43# subset (mutants); RX_OP_FAILFAST=1 stops at the first failed check (host);
44# RX_OP_OUT (default build/r16-operator) receives the receipt and the logs of
45# a failed run.
46set -u
47SEAT=${1:?host or silicon}; BIN=${2:?binary}; CLI=${3:?client}
48case "$SEAT" in host|silicon) ;; *) echo "seat must be host or silicon" >&2; exit 2 ;; esac
49HERE=$(cd "$(dirname "${BASH_SOURCE[0]}")/../.." && pwd)
50BIN=$(cd "$(dirname "$BIN")" && pwd)/$(basename "$BIN")
51CLI=$(cd "$(dirname "$CLI")" && pwd)/$(basename "$CLI")
52PHASES=${RX_OP_PHASES:-"startup control restart kill"}
53FAILFAST=${RX_OP_FAILFAST:-0}
54OUT=${RX_OP_OUT:-$HERE/build/r16-operator}
55W=$(mktemp -d "${TMPDIR:-/tmp}/rx-op.XXXXXX"); chmod 700 "$W"
56if [ "$SEAT" = silicon ]; then WORLD_WAIT=900; EXIT_WAIT=3600; else WORLD_WAIT=120; EXIT_WAIT=300; fi
57CHECKS=0; FAILS=0; SKIPS=0; SKIP_WHY=(); PID=""; GPID=""
58SUDO=0; if sudo -n true 2>/dev/null; then SUDO=1; fi
59HOLD=0; if [ "$SEAT" = host ] && command -v gdb >/dev/null 2>&1; then HOLD=1; fi
60
61cleanup() {
62 if [ -n "$PID" ] && kill -0 "$PID" 2>/dev/null; then
63 if [ "$SEAT" = host ]; then kill -9 "$PID" 2>/dev/null; wait "$PID" 2>/dev/null
64 else "$CLI" --control "$S/control" resume >/dev/null 2>&1; wait "$PID" 2>/dev/null; fi
65 fi
66 if [ -n "$GPID" ]; then echo quit 2>/dev/null >&7; sleep 0.2; kill -9 "$GPID" 2>/dev/null; wait "$GPID" 2>/dev/null; fi
67 [ "$SUDO" = 1 ] && sudo -n rm -rf "$W" 2>/dev/null || rm -rf "$W"
68}
69trap cleanup EXIT
70
71receipt() {
72 local commit dirty sum why="" r
73 mkdir -p "$OUT"
74 commit=$(git -C "$HERE" rev-parse HEAD 2>/dev/null || echo unknown)
75 if [ -n "$(git -C "$HERE" status --porcelain 2>/dev/null)" ]; then dirty=true; else dirty=false; fi
76 sum=$(sha256sum "$BIN" | cut -d' ' -f1)
77 for r in "${SKIP_WHY[@]+"${SKIP_WHY[@]}"}"; do why="$why${why:+, }\"$r\""; done
78 cat > "$OUT/operator_${SEAT}_receipt.json" <<EOF
79{
80 "schema": "AIEN_R16_G6_OPERATOR_CONTROL_V1",
81 "seat": "$SEAT",
82 "seat_description": "$([ "$SEAT" = silicon ] && echo "production GB10 program, resident Blackwell seat" || echo "production host program, R12 processor stand-in (not silicon)")",
83 "candidate_commit": "$commit",
84 "candidate_env": "${OMEGA_CANDIDATE_COMMIT:-}",
85 "tree_dirty": $dirty,
86 "binary": "$(basename "$BIN")",
87 "binary_sha256": "$sum",
88 "phases": "$PHASES",
89 "held_by_gdb": $([ "$HOLD" = 1 ] && echo true || echo false),
90 "checks": $CHECKS,
91 "failures": $FAILS,
92 "skipped": $SKIPS,
93 "skip_reasons": [$why],
94 "gate": {"R16_G6_OPERATOR": "$1"}
95}
96EOF
97 echo "R16 operator receipt: $OUT/operator_${SEAT}_receipt.json"
98}
99
100finish() {
101 local gate
102 if [ "$FAILS" -gt 0 ]; then gate=FAIL
103 elif [ "$SKIPS" -gt 0 ]; then gate="NOT_RUN ($SKIPS SKIPPED)"
104 elif [ "$SEAT" = silicon ]; then gate=PASS
105 else gate=HOST_PASS_NON_SILICON; fi
106 receipt "$gate"
107 if [ "$FAILS" -gt 0 ]; then # keep the program logs of a failed run for diagnosis
108 local keep="$OUT/failed-$SEAT-$(date -u +%Y%m%dT%H%M%SZ)-$$"
109 mkdir -p "$keep" && cp "$W"/*.log "$keep"/ 2>/dev/null
110 echo "R16 operator: program logs kept in $keep"
111 fi
112 echo "R16 operator: $CHECKS checks, $FAILS failures, $SKIPS skipped ($SEAT, phases: $PHASES)"
113 echo "R16 operator gate: R16_G6_OPERATOR=$gate"
114 [ "$FAILS" = 0 ]
115 exit $?
116}
117
118check() { # check <label> <shell condition>
119 CHECKS=$((CHECKS + 1))
120 if eval "$2"; then echo "[+] $1"
121 else
122 echo "[-] $1"; FAILS=$((FAILS + 1))
123 echo " last reply (client exit $OPRC): $(printf '%s' "$REPLY_" | head -c 300)"
124 if [ "$FAILFAST" = 1 ]; then finish; fi
125 fi
126}
127skip() { echo "[SKIPPED] $1"; SKIPS=$((SKIPS + 1)); SKIP_WHY+=("$1"); }
128
129REPLY_=""; OPRC=0
130op() { REPLY_=$("$CLI" "$@" 2>&1); OPRC=$?; return $OPRC; } # op <client args>: sets REPLY_, OPRC
131kv() { printf '%s\n' "$REPLY_" | tr ' ' '\n' | sed -n "s/^$1=//p" | head -1; }
132has() { printf '%s\n' "$REPLY_" | grep -Fq -- "$1"; }
133
134start_prog() { # start_prog <state dir> <log>
135 "$BIN" --state-dir "$1" > "$2" 2>&1 < /dev/null &
136 PID=$!
137}
138# start_held <state dir> <log>: the program under gdb (host), PID = the program itself.
139start_held() {
140 local g="$W/hold.gdb" f="$W/hold.fifo" i=0
141 cat > "$g" <<'EOF'
142set debuginfod enabled off
143set pagination off
144set confirm off
145set non-stop on
146set startup-with-shell off
147set print thread-events off
148set print inferior-events on
149handle SIGPIPE nostop noprint pass
150tbreak rx_resident_accept
151tbreak rx_world_verify_crumbs
152tbreak rx_operator_open
153ignore 3 1
154run
155EOF
156 mkfifo -m 600 "$f" && exec 7<>"$f"
157 gdb -nx -q -x "$g" --args "$BIN" --state-dir "$1" <&7 > "$2" 2>&1 &
158 GPID=$!; PID=""
159 while [ $i -lt $((WORLD_WAIT * 20)) ]; do
160 PID=$(sed -n 's/^R13 operator: state .* pid \([0-9][0-9]*\))$/\1/p' "$2" | head -1)
161 [ -n "$PID" ] && return 0
162 kill -0 "$GPID" 2>/dev/null || return 1
163 sleep 0.05; i=$((i + 1))
164 done
165 return 1
166}
167held() { # held <n>: the program's thread is paused at temporary breakpoint n
168 local i=0
169 while [ $i -lt $((WORLD_WAIT * 20)) ]; do
170 grep -Eq "hit Temporary breakpoint $1, " "$LA" && return 0
171 alive || return 1
172 sleep 0.05; i=$((i + 1))
173 done
174 return 1
175}
176release() { echo "continue -a" >&7; } # resume every paused thread
177alive() { [ -n "$PID" ] && kill -0 "$PID" 2>/dev/null; }
178# wait_world <state> <mode-key>: this program start's credential file for that world is
179# there (a file left by a killed earlier start names another pid) and its socket exists.
180wait_world() {
181 local i=0 lim=$((WORLD_WAIT * 50))
182 while [ $i -lt $lim ]; do
183 if grep -q "^world $2 $PID " "$1/control/operator.cred" 2>/dev/null && [ -S "$1/control/operator.sock" ]; then
184 return 0
185 fi
186 alive || return 1
187 sleep 0.02; i=$((i + 1))
188 done
189 return 1
190}
191wait_line() { # wait_line <log> <fixed text> [seconds]: until the program printed it (or exited)
192 local i=0 lim=$((${3:-$WORLD_WAIT} * 20))
193 while [ $i -lt $lim ]; do
194 grep -Fq -- "$2" "$1" && return 0
195 alive || { grep -Fq -- "$2" "$1"; return $?; }
196 sleep 0.05; i=$((i + 1))
197 done
198 return 1
199}
200# wait_exit: the program ends by itself. A program still running after EXIT_WAIT
201# seconds (a world left stopped by a failed check) is a failure: on host it is
202# killed, on silicon it is resumed and waited for (a chip program is never killed).
203# Under gdb the exit status is the one gdb reports for the program.
204wait_exit() {
205 local i=0 lim=$((EXIT_WAIT * 20)) late=0 code
206 while alive && [ $i -lt $lim ]; do sleep 0.05; i=$((i + 1)); done
207 if alive; then
208 late=1; echo "program still running after ${EXIT_WAIT}s"
209 if [ "$SEAT" = host ]; then kill -9 "$PID" 2>/dev/null
210 else "$CLI" --control "$S/control" resume >/dev/null 2>&1; fi
211 fi
212 if [ -n "$GPID" ]; then
213 i=0; while [ $i -lt 200 ] && ! grep -Eq '^\[Inferior 1 \(process [0-9]+\) exited|terminated with signal' "$LA"; do sleep 0.05; i=$((i + 1)); done
214 if grep -Eq '^\[Inferior 1 \(process [0-9]+\) exited normally\]' "$LA"; then RC=0
215 else
216 code=$(sed -n 's/^\[Inferior 1 (process [0-9]*) exited with code \([0-7]*\)\]$/\1/p' "$LA" | head -1)
217 if [ -n "$code" ]; then RC=$((8#$code)); else RC=137; fi
218 fi
219 echo quit >&7; wait "$GPID" 2>/dev/null; exec 7>&-; GPID=""
220 else
221 wait "$PID"; RC=$?
222 fi
223 PID=""
224 [ $late = 0 ] || RC=124
225}
226# dup_run <state dir> <log>: start a second program and wait up to 20 s for it to exit;
227# sets rc (124 if it was still running: the lock did not refuse it). Host: it is then
228# killed. Silicon: it is stopped and shut down through its own entry point (a chip
229# program is never killed).
230dup_run() {
231 "$BIN" --state-dir "$1" > "$2" 2>&1 < /dev/null &
232 local d=$! i=0
233 while kill -0 "$d" 2>/dev/null && [ $i -lt 400 ]; do sleep 0.05; i=$((i + 1)); done
234 if kill -0 "$d" 2>/dev/null; then
235 echo "a second program on $1 is still running after 20 s"
236 if [ "$SEAT" = host ]; then kill -9 "$d" 2>/dev/null
237 else "$CLI" --control "$1/control" stop >/dev/null 2>&1; "$CLI" --control "$1/control" shutdown >/dev/null 2>&1; fi
238 wait "$d" 2>/dev/null; rc=124; return
239 fi
240 wait "$d"; rc=$?
241}
242sealed() { # sealed <file>: last line is "sha256 <64 hex>"
243 [ -f "$1" ] && tail -n 1 "$1" | grep -Eq '^sha256 [0-9a-f]{64}$'
244}
245forge() { # forge <cred in> <cred out> <sed expression>
246 sed -e "$3" "$1" > "$2"; chmod 600 "$2"
247}
248audit_ok() { # audit_ok <log> <mode-key> <stops> <resumes> <restored>
249 grep -Eq "^R13 operator $2: stops $3 resumes $4 restored $5 open 0; under stop: commits 0 externals 0 cancelled [0-9]+; order ok$" "$1"
250}
251# raw <cred file> <command> [fields | -]: a request line with that credential and a
252# deadline the client fills in (@deadline@), or the given fields, or no field (-).
253raw() {
254 local c=$1 cmd=$2; shift 2
255 local f; f=$(sed -n 's/^cred \([0-9]*\) \([0-9a-f]*\)$/gen=\1 secret=\2/p' "$c")
256 local k; k=$(sed -n 's/^cap \([0-9]*\) \([0-9]*\)$/cap=\1:\2/p' "$c")
257 local s; s=$(sed -n 's/^subject \([0-9]*\)$/subject=\1/p' "$c")
258 local x="${*:-deadline=@deadline@}"; [ "$x" = - ] && x=""
259 echo "aien-operator v1 $cmd $s $f $k${x:+ $x}"
260}
261
262echo "R16 operator control ($SEAT): binary $BIN"
263
264# ---- startup: the state directory is validated before any world starts -------------
265if [[ " $PHASES " == *" startup "* ]]; then
266 mkdir -m 755 "$W/open755"
267 "$BIN" --state-dir "$W/open755" > "$W/s1.log" 2>&1; rc=$?
268 check "startup: a group/other-readable state directory is refused (exit 2) before any world" \
269 '[ $rc = 2 ] && grep -q "state directory refused" "$W/s1.log" && [ ! -e "$W/open755/control" ]'
270 mkdir -m 700 "$W/real"; ln -s "$W/real" "$W/link"
271 "$BIN" --state-dir "$W/link" > "$W/s2.log" 2>&1; rc=$?
272 check "startup: a symlinked state directory is refused" '[ $rc = 2 ] && grep -q "is a symlink" "$W/s2.log"'
273 mkdir -m 700 "$W/ctl"; mkdir -m 750 "$W/ctl/control"
274 "$BIN" --state-dir "$W/ctl" > "$W/s3.log" 2>&1; rc=$?
275 check "startup: a group-readable control directory is refused" '[ $rc = 2 ] && grep -q "control directory refused" "$W/s3.log"'
276 if [ "$SUDO" = 1 ]; then
277 mkdir -m 700 "$W/foreign"; sudo -n chown root "$W/foreign"
278 "$BIN" --state-dir "$W/foreign" > "$W/s4.log" 2>&1; rc=$?
279 check "startup: a state directory owned by another uid is refused" '[ $rc = 2 ] && grep -q "owned by uid 0" "$W/s4.log"'
280 else
281 skip "startup: foreign-owned state directory needs sudo -n to chown (no passwordless sudo here)"
282 fi
283fi
284
285# ---- control: one program start driven through four of its worlds --------------------
286S="$W/state-a"; mkdir -m 700 "$S"; LA="$W/run-a.log"; SEQ=0
287if [[ " $PHASES " == *" control "* ]] || [[ " $PHASES " == *" restart "* ]]; then
288 if [ "$HOLD" = 1 ] && [[ " $PHASES " == *" control "* ]]; then
289 check "control: the production program starts under gdb (non-stop holds)" 'start_held "$S" "$LA"'
290 else
291 start_prog "$S" "$LA"
292 fi
293 check "control: the positive world opens the operator entry point" 'wait_world "$S" positive'
294 C="$S/control"; cp "$C/operator.cred" "$W/cred-positive"
295 forge "$W/cred-positive" "$W/forged" 's/^\(cred [0-9]* \)\(.\)\(.*\)$/\1\3\2/' # rotated secret: right length, wrong bytes
296 forge "$W/cred-positive" "$W/wrongcap" 's/^cap \([0-9]*\) /cap 1\1 /'
297 forge "$W/cred-positive" "$W/wrongsubj" 's/^subject 70$/subject 61/'
298fi
299if [[ " $PHASES " == *" control "* ]]; then
300 [ "$SEAT" = host ] && [ "$HOLD" = 0 ] && skip "control: the deterministic in-flight, end-of-episode and close holds need gdb (not installed)"
301 # Authorized stop, sent the moment the world exists: the program answers it as soon
302 # as setup is done, before the episode has done its work, so the promotion is still
303 # ahead and cannot happen under the stop.
304 op --control "$C" stop 7; SEQ=1
305 check "stop: authorized stop accepted, durable, the first stop of this world (client exit 0)" \
306 '[ $OPRC = 0 ] && has "OK state=stopped" && [ "$(kv seq)" = 1 ] && [ "$(kv durable)" = 1 ]'
307 M="$S/gen-positive/OPERATOR_HALT"
308 check "stop: sealed mark in the generation store directory" \
309 'head -n 1 "$M" | grep -qx "aien-operator-halt v1" && grep -qx "reason 7" "$M" && grep -qx "subject 70" "$M" && sealed "$M"'
310 op --control "$C" status
311 sv1=$(kv served); pc1=$(kv production_commits); ag1=$(kv active_generation); pr1=$(kv promotion); if1=$(kv inforce)
312 check "stop: status says stopped, the promotion has not happened yet" 'has "state=stopped" && [ "$pr1" = 0 ] && [ "$if1" = 0 ] && [ -n "$sv1" ]'
313
314 # One program per state directory (S1): a second program on the same state, or on
315 # this program's generation (halt) directory, is refused before it touches the
316 # socket or the credential, and the first program stays controllable.
317 ino1=$(stat -c %i "$C/operator.sock"); cr1=$(sha256sum < "$C/operator.cred")
318 dup_run "$S" "$W/dup1.log"
319 check "lock: a second program on the same state directory is refused (exit 2, names the holder)" \
320 '[ $rc = 2 ] && grep -q "state directory refused: .* is in use by another program (pid $PID)" "$W/dup1.log"'
321 dup_run "$S/gen-positive" "$W/dup2.log"
322 check "lock: a second program on the first one's generation directory is refused" \
323 '[ $rc = 2 ] && grep -q "in use by another program (pid $PID)" "$W/dup2.log" && [ ! -e "$S/gen-positive/control" ]'
324 op --control "$C" status
325 check "lock: the first program keeps its socket and credential and stays controllable" \
326 'has "OK state=stopped" && [ "$(kv seq)" = 1 ] && [ "$(stat -c %i "$C/operator.sock")" = "$ino1" ] && [ "$(sha256sum < "$C/operator.cred")" = "$cr1" ]'
327
328 # Unauthorized requests: each refused, nothing changes, no state revealed.
329 op --cred "$W/forged" --control "$C" stop
330 check "unauthorized: forged secret, stop REFUSED identity" 'has "REFUSED reason=identity" && ! has state='
331 op --cred "$W/forged" --control "$C" status
332 check "unauthorized: forged secret, status REFUSED identity (no state revealed)" 'has "REFUSED reason=identity" && ! has state='
333 op --cred "$W/wrongcap" --control "$C" stop
334 check "unauthorized: another capability, stop REFUSED authority" 'has "REFUSED reason=authority"'
335 op --cred "$W/wrongsubj" --control "$C" stop
336 check "unauthorized: a reaction subject (61) with the operator secret, REFUSED identity" 'has "REFUSED reason=identity"'
337 op --control "$C" --raw "aien-operator v1 stop"
338 check "unauthorized: request without credential fields is BAD_REQUEST" 'has "BAD_REQUEST"'
339 # S3: the socket routes the operator capability only to stop, resume and the
340 # authorized status/shutdown/revoke commands. Anything else, with a full valid
341 # credential, is refused before any authority call.
342 bad=""
343 for c in explode bump-epoch bump_epoch mint grant epoch STOP Stop stop2 resume2 halt; do
344 op --control "$C" --raw "$(raw "$W/cred-positive" "$c")"
345 has "aien-operator v1 BAD_REQUEST" || bad="$bad $c"
346 done
347 check "unknown commands with a full credential are BAD_REQUEST (11 commands${bad:+, accepted:$bad})" '[ -z "$bad" ]'
348 op --control "$C" --raw "$(raw "$W/cred-positive" resume reason=1 deadline=@deadline@)"
349 check "unauthorized: a field the command does not take (resume reason=) is BAD_REQUEST" 'has "BAD_REQUEST"'
350 if [ "$SUDO" = 1 ]; then
351 REPLY_=$(sudo -n "$CLI" --control "$C" stop 2>&1)
352 check "unauthorized: a peer of another uid (root) is REFUSED peer, even with the credential" 'has "REFUSED reason=peer"'
353 else
354 skip "unauthorized: a peer of another uid needs sudo -n (no passwordless sudo here)"
355 fi
356 op --cred "$W/forged" --control "$C" resume
357 check "unauthorized: a forged resume is REFUSED" 'has "REFUSED reason=identity"'
358 op --cred "$W/wrongcap" --control "$C" resume
359 check "unauthorized: a resume with another capability is REFUSED" 'has "REFUSED reason=authority"'
360 op --cred "$W/wrongsubj" --control "$C" resume
361 check "unauthorized: a resume by a reaction subject is REFUSED" 'has "REFUSED reason=identity"'
362 # S4: a request whose client already gave up is never run late (resume), and a
363 # request without a deadline, or with one beyond the client's wait, is malformed.
364 op --control "$C" --raw "$(raw "$C/operator.cred" resume deadline=1)"
365 check "expired: a resume whose client deadline passed answers EXPIRED and does nothing" 'has "aien-operator v1 EXPIRED"'
366 op --control "$C" --raw "$(raw "$C/operator.cred" resume deadline=18000000000000000000)"
367 check "expired: a deadline beyond the client's wait is BAD_REQUEST" 'has "BAD_REQUEST"'
368 op --control "$C" --raw "$(raw "$C/operator.cred" resume -)"
369 check "expired: a resume without a deadline is BAD_REQUEST" 'has "BAD_REQUEST"'
370 sleep 2
371 op --control "$C" status
372 check "stop: after every refusal and two seconds, still the same stop; nothing served, committed or promoted" \
373 'has "state=stopped" && [ "$(kv seq)" = 1 ] && [ "$(kv served)" = "$sv1" ] && [ "$(kv production_commits)" = "$pc1" ] && [ "$(kv active_generation)" = "$ag1" ] && [ "$(kv promotion)" = 0 ] && [ "$(kv inforce)" = 0 ] && [ -e "$M" ]'
374 op --control "$C" stop 8
375 check "repeat: a second stop answers ALREADY, same stop" 'has "ALREADY state=stopped" && [ "$(kv seq)" = 1 ]'
376 op --control "$C" resume
377 check "resume: authorized resume accepted" 'has "OK state=running" && [ "$(kv seq)" = 1 ]'
378 check "resume: the mark is gone and the sealed resumed record kept" \
379 '[ ! -e "$M" ] && R=$(ls "$S"/gen-positive/OPERATOR_HALT.resumed.1.* 2>/dev/null | head -1) && [ -n "$R" ] && grep -qx "resumed_by 70" "$R" && grep -qx "reason 7" "$R" && sealed "$R"'
380 op --control "$C" resume
381 check "repeat: resume of a running world answers NOT_STOPPED" 'has "NOT_STOPPED"'
382 op --control "$C" shutdown
383 check "shutdown of a running world answers NOT_STOPPED (the program goes on)" 'has "NOT_STOPPED"'
384
385 # S2: a stop that cannot be written to disk is in force in memory but would not
386 # survive a restart. Its reply and the client's exit code say so.
387 if [ "$(id -u)" != 0 ]; then
388 chmod 500 "$S/gen-positive"
389 op --control "$C" stop 12; SEQ=2
390 chmod 700 "$S/gen-positive"
391 check "not durable: a stop whose mark cannot be written answers STOPPED_NOT_DURABLE errno=13, client exit 3" \
392 '[ $OPRC = 3 ] && has "STOPPED_NOT_DURABLE state=stopped" && [ "$(kv seq)" = 2 ] && [ "$(kv durable)" = -13 ] && [ "$(kv errno)" = 13 ] && [ ! -e "$M" ]'
393 op --control "$C" status
394 check "not durable: the stop is in force (status stopped, durable -13)" 'has "OK state=stopped" && [ "$(kv durable)" = -13 ]'
395 op --control "$C" resume
396 check "not durable: resume accepted" 'has "OK state=running" && [ "$(kv seq)" = 2 ]'
397 else
398 skip "not durable: an unwritable halt directory cannot be made as root"
399 fi
400 STOPS=$SEQ
401
402 # In flight (G6), host: the acceptor thread is paused holding a computed seat
403 # result, the stop is accepted, then the thread goes on: the result must be
404 # refused at commit and the seat claim end halted, every run.
405 if [ "$HOLD" = 1 ]; then
406 check "in-flight: the seat acceptor is paused holding a computed seat result (breakpoint 1)" 'held 1'
407 op --cred "$W/cred-positive" --control "$C" stop 9; SEQ=$((SEQ + 1)); STOPS=$SEQ
408 check "in-flight: the stop is accepted while that activation is in flight" 'has "OK state=stopped" && [ "$(kv seq)" = $SEQ ]'
409 release
410 ok=0; i=0
411 while [ $i -lt 200 ]; do # waits for the released thread, never repeats the stop
412 op --cred "$W/cred-positive" --control "$C" status
413 [ "$(kv seat_halted)" -ge 1 ] 2>/dev/null && { ok=1; break; }; sleep 0.05; i=$((i + 1))
414 done
415 check "in-flight: the held result was refused at commit (cancelled $(kv cancelled), seat_halted $(kv seat_halted))" \
416 '[ $ok = 1 ] && [ "$(kv cancelled)" -ge 1 ] && has "state=stopped"'
417 op --cred "$W/cred-positive" --control "$C" resume
418 check "in-flight: resume accepted" 'has "OK state=running"'
419 fi
420 ok=0
421 for i in $(seq 1 200); do op --control "$C" status; [ "$(kv served)" != "$sv1" ] && { ok=1; break; }; sleep 0.05; done
422 check "resume: production serves again after the resume" '[ $ok = 1 ]'
423 op --cred "$W/forged" --control "$C" stop
424 check "unauthorized: a forged stop of the running world is REFUSED identity" 'has "REFUSED reason=identity"'
425 op --cred "$W/wrongcap" --control "$C" stop
426 check "unauthorized: a stop of the running world with another capability is REFUSED authority" 'has "REFUSED reason=authority"'
427 op --control "$C" status
428 check "unauthorized: after the refused stops the world still runs, no new stop, no mark" \
429 'has "OK state=running" && [ "$(kv seq)" = $SEQ ] && [ ! -e "$M" ]'
430
431 # Under production load: 12 stop/resume cycles. Every request carries the positive
432 # world's own credential, so a stop can never land in the next world. On host the
433 # positive world cannot end during them (breakpoint 2 holds its end), so all 12 run.
434 cycles=0
435 for i in $(seq 1 12); do
436 op --cred "$W/cred-positive" --control "$C" stop 9 || break
437 has "OK state=stopped" || break
438 sleep 0.05
439 op --cred "$W/cred-positive" --control "$C" resume || break
440 has "OK state=running" || break
441 cycles=$((cycles + 1))
442 done
443 STOPS=$((STOPS + cycles))
444 if [ "$HOLD" = 1 ]; then
445 check "load: 12 of 12 stop/resume cycles while the positive world runs ($cycles)" '[ $cycles = 12 ]'
446 else
447 check "load: at least 6 of 12 stop/resume cycles while the positive world runs ($cycles)" '[ $cycles -ge 6 ]'
448 fi
449
450 if [ "$HOLD" = 1 ]; then
451 # End of episode (G5 M-a): the positive episode's work is done and its world is
452 # paused before teardown. A stop there must keep the world up, stopped, until
453 # the operator resumes it.
454 check "end of episode: the positive world's work is done, the world is paused before teardown (breakpoint 2)" 'held 2'
455 op --cred "$W/cred-positive" --control "$C" stop 10; STOPS=$((STOPS + 1))
456 check "end of episode: the stop is accepted" 'has "OK state=stopped"'
457 release
458 check "end of episode: the program says it waits for an operator resume" \
459 'wait_line "$LA" "R13 operator positive: stopped after its episode; waiting for an operator resume" 10'
460 sleep 2
461 op --cred "$W/cred-positive" --control "$C" status
462 check "end of episode: two seconds later the positive world is still up and stopped, not audited, not ended" \
463 'has "OK state=stopped" && has "world=positive" && ! grep -q "^R13 operator positive: stops" "$LA"'
464 # Close removes only what this instance made: a credential file another program
465 # put in its place must still be there after the positive world closed.
466 printf 'aien-operator-credential v1\nforeign %s\n' "$$" > "$C/.plant"; chmod 600 "$C/.plant"
467 mv -f "$C/.plant" "$C/operator.cred"
468 op --cred "$W/cred-positive" --control "$C" resume
469 check "end of episode: resume accepted" 'has "OK state=running"'
470 check "close: the next world's open is paused after the positive world closed (breakpoint 3)" 'held 3'
471 check "close: the positive world's close left the credential file it did not create" \
472 'grep -qx "foreign $$" "$C/operator.cred"'
473 release
474 fi
475 check "positive world: the promotion happens after the resume (generation advanced)" 'wait_line "$LA" "R13 positive: generation"'
476 check "positive world: crumb log shows $STOPS stops, as many resumes, nothing committed or published under a stop" \
477 'audit_ok "$LA" positive $STOPS $STOPS 0'
478 cancelled=$(sed -n 's/^R13 operator positive: .*cancelled \([0-9]*\);.*$/\1/p' "$LA")
479 if [ "$HOLD" = 1 ]; then
480 check "in-flight: the crumb log counts the cancelled activations (${cancelled:-?})" '[ "${cancelled:-0}" -ge 1 ]'
481 else
482 echo "in-flight ($SEAT, informational): activations cancelled by a stop: ${cancelled:-?}"
483 fi
484
485 # Next world (A no AIEN): stale credential, then the capability revoked by the authority.
486 check "rotation: the next world opens with a new credential" 'wait_world "$S" no-aien'
487 check "rotation: the new credential differs from the earlier world's" '! cmp -s "$W/cred-positive" "$C/operator.cred"'
488 op --cred "$W/cred-positive" --control "$C" status
489 check "stale: the earlier world's credential is REFUSED identity" 'has "REFUSED reason=identity"'
490 op --control "$C" revoke-cap
491 check "revoke-cap: the authority revokes the control capability" 'has "OK revoked=capability"'
492 op --control "$C" stop
493 check "revoke-cap: a stop with the revoked capability is REFUSED authority" 'has "REFUSED reason=authority"'
494 op --control "$C" status
495 check "revoke-cap: status with it is REFUSED authority" 'has "REFUSED reason=authority"'
496 check "revoke-cap: the world runs on without operator control (control A passes, no stop)" \
497 'wait_line "$LA" "R13 control A:" && audit_ok "$LA" no-aien 0 0 0 && [ ! -e "$S/gen-no-aien/OPERATOR_HALT" ]'
498
499 # Next world (B no promotion authority): the credential revoked.
500 check "revoke: world B opens" 'wait_world "$S" no-promotion'
501 cp "$C/operator.cred" "$W/cred-b"
502 op --control "$C" revoke
503 check "revoke: capability and credential revoked, credential file removed" 'has "OK revoked=capability,credential" && [ ! -e "$C/operator.cred" ]'
504 op --cred "$W/cred-b" --control "$C" stop
505 check "revoke: a stop with the revoked credential is REFUSED identity" 'has "REFUSED reason=identity"'
506 check "revoke: world B completes without a stop" 'wait_line "$LA" "R13 control B:" && audit_ok "$LA" no-promotion 0 0 0'
507fi
508if [[ " $PHASES " == *" control "* ]] || [[ " $PHASES " == *" restart "* ]]; then
509 # World C: stop, then shut the program down while stopped.
510 check "shutdown: world C opens" 'wait_world "$S" revoked-experiment'
511 cp "$C/operator.cred" "$W/cred-c"
512 op --control "$C" stop 11
513 check "shutdown: stop world C" 'has "OK state=stopped"'
514 op --cred "$W/forged" --control "$C" shutdown
515 check "shutdown: a forged shutdown is REFUSED" 'has "REFUSED reason=identity"'
516 op --control "$C" shutdown
517 check "shutdown: authorized shutdown of the stopped world" 'has "OK state=stopped shutdown=1"'
518 wait_exit
519 check "shutdown: the program exits 3, writes no receipt, the stop stays on disk" \
520 '[ $RC = 3 ] && grep -q "shutdown while stopped in revoked-experiment" "$LA" && ! grep -q "R13 gate:" "$LA" && sealed "$S/gen-revoked-experiment/OPERATOR_HALT" && [ ! -e "$C/operator.sock" ]'
521fi
522
523# ---- restart while stopped (after the shutdown) -----------------------------------------
524if [[ " $PHASES " == *" restart "* ]]; then
525 LB="$W/run-b.log"
526 start_prog "$S" "$LB"
527 check "restart: world C reopens" 'wait_world "$S" revoked-experiment'
528 op --control "$C" status
529 check "restart: world C comes up STOPPED, restored, before any work (no reaction registered)" \
530 'has "OK state=stopped" && [ "$(kv restored)" = 1 ] && [ "$(kv reactions)" = 0 ] && has "setup=pending"'
531 k1=$(kv crumbs)
532 check "restart: the program says the stop was restored" 'grep -q "^R13 operator revoked-experiment: restored STOPPED" "$LB"'
533 op --cred "$W/cred-c" --control "$C" status
534 check "stale: the credential of the earlier program start is REFUSED identity" 'has "REFUSED reason=identity"'
535 sleep 1
536 op --control "$C" status
537 check "restart: one second later still stopped, nothing built, no crumb added" \
538 'has "state=stopped" && [ "$(kv reactions)" = 0 ] && [ "$(kv crumbs)" = "$k1" ]'
539 op --control "$C" resume
540 check "recovery: resume of the restored stop" 'has "OK state=running"'
541 check "recovery: the mark is gone, the resumed record kept" \
542 '[ ! -e "$S/gen-revoked-experiment/OPERATOR_HALT" ] && ls "$S"/gen-revoked-experiment/OPERATOR_HALT.resumed.* >/dev/null 2>&1'
543 wait_exit
544 # Host: the stand-in is never silicon. Silicon: only a full PASS (bound to the
545 # candidate commit, clean tree, silicon observed) counts; the spec allows no other.
546 if [ "$SEAT" = host ]; then want="R13 gate: R13_LIVING_SYSTEM=HOST_PASS_NON_SILICON"; else want="R13 gate: R13_LIVING_SYSTEM=PASS"; fi
547 check "recovery: the program completes every mode and its R13 gate is ${want#R13 gate: } (exit 0)" \
548 '[ $RC = 0 ] && grep -q "R13 control C:" "$LB" && grep -Eq "^${want}( |$)" "$LB"'
549 check "recovery: crumb log of world C shows the restored stop and its resume, nothing under it" \
550 'audit_ok "$LB" revoked-experiment 1 1 1'
551fi
552
553# ---- crash while stopped (host only: a chip program is never killed) ------------------
554if [[ " $PHASES " == *" kill "* ]]; then
555 if [ "$SEAT" = host ]; then
556 S="$W/state-k"; mkdir -m 700 "$S"; C="$S/control"; LC="$W/run-c.log"; LD="$W/run-d.log"
557 start_prog "$S" "$LC"
558 check "crash: positive world opens" 'wait_world "$S" positive'
559 op --control "$C" stop 13
560 check "crash: stop accepted" 'has "OK state=stopped"'
561 kill -9 "$PID"; wait "$PID" 2>/dev/null; PID=""
562 check "crash: after SIGKILL the sealed mark is on disk" 'sealed "$S/gen-positive/OPERATOR_HALT" && grep -qx "reason 13" "$S/gen-positive/OPERATOR_HALT"'
563 start_prog "$S" "$LD"
564 check "crash restart: positive world reopens (the dead program's lock is gone with it)" 'wait_world "$S" positive'
565 op --control "$C" status
566 check "crash restart: comes up STOPPED before any work" 'has "OK state=stopped" && [ "$(kv restored)" = 1 ] && [ "$(kv reactions)" = 0 ]'
567 op --control "$C" resume
568 check "crash recovery: resume accepted" 'has "OK state=running"'
569 wait_exit
570 check "crash recovery: positive promotes after the restart, every mode passes (exit 0)" \
571 '[ $RC = 0 ] && grep -q "R13 positive: generation" "$LD" && grep -Fq "R13 gate: R13_LIVING_SYSTEM=HOST_PASS_NON_SILICON" "$LD" && audit_ok "$LD" positive 1 1 1'
572 else
573 echo "kill phase: host only by design (a chip program is never killed); restart on silicon is proven by the shutdown phase"
574 fi
575fi
576
577finish