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 / own30b-pkv-ops-check.sh

197 lines · 10098 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# Persistent KV operations check on the chip (sc#509, contract locked in #500). CHIP-ONLY: never
18# run by CI or cargo test. Same safety rules as own30b-pkv-chip-check.sh (see own30b-pkv-lib.sh):
19# own server on 127.0.0.1:18098 (PKV_CHECK_PORT), refuses while :18091 answers or the port is
20# taken, stops only the PID it started, key via `atlas-vault run AIEN_KV_STORE_KEY --` (never
21# printed), evidence sealed with SHA256SUMS.
22#
23# scripts/own30b-pkv-ops-check.sh [--dry-run] <evidence-dir> <aien-serve-http binary>
24#
25# Phases (the #507 greedy two-turn conversation, temperature 0, seed 1):
26# off AIEN_KV_STORE_DIR unset: no persistent_kv in health, no "persistent KV" log line
27# -> T_off (turn 2 uninterrupted)
28# on opt in with a store dir D that does not exist yet: health on, store open,
29# recovery done, every counter present; turn 2 == T_off; after the parks every dir
30# under D is 0700 and every file 0600
31# sigterm on the same server: a 256-token streamed chat, SIGTERM once its first token is
32# out: the chat still ends with [DONE], the server exits 0 within PKV_STOP_WAIT_S
33# (120), logs "shutdown complete", and leaves no temp file in D
34# restart restart on D: turn 2 restores once and == T_off, recovery indexed the objects
35# corrupt flip a byte in every object of D, add a foreign file and a stray temp file:
36# starts and serves, restores 0, turn 2 == T_off, recovery removed the temp
37# foreign restart on D with another valid key (a store written by someone else): restores 0,
38# turn 2 == T_off
39# unsafe a store dir with mode 0755: persistence refused at startup (refusals 1), the
40# server still serves, turn 2 == T_off, the dir stays empty
41# nokey AIEN_KV_STORE_DIR set without AIEN_KV_STORE_KEY: refused, still serves == T_off
42# rollback unset AIEN_KV_STORE_DIR and restart: health has no persistent_kv, turn 2 == T_off,
43# and D is byte-for-byte untouched (SHA-256 of every file before and after)
44# Rough duration: 8 server starts, 9 to 15 minutes.
45set -euo pipefail
46. "$(dirname "$0")/own30b-pkv-lib.sh"
47pkv_usage_args "$(basename "$0")" "$@"
48
49if [ "$DRY" = 1 ]; then
50 echo "PLAN own30b-pkv-ops-check (#509): off, on (fresh dir, 0700/0600), sigterm (graceful drain),"
51 echo " restart (restores), corrupt + foreign/stray files, foreign key, unsafe dir (0755), no key, rollback"
52 echo " 8 server starts on :$PORT, estimate 9 to 15 minutes; evidence -> $dir"
53 echo "PREREQUISITES"
54 if pkv_prereqs 1; then echo "DRY RUN: ready"; exit 0; fi
55 echo "DRY RUN: NOT ready"
56 exit 1
57fi
58pkv_begin
59
60perm_bad() { # <dir>: entries that are not 0700 dirs / 0600 files
61 find "$1" \( -type d ! -perm 700 \) -o \( -type f ! -perm 600 \) | wc -l
62}
63tree_sums() { (cd "$1" && find . -type f -print0 | sort -z | xargs -0 -r sha256sum); }
64
65echo "== off: AIEN_KV_STORE_DIR unset"
66start_server off -
67a1_off=$(chat off-turn1 "$m1")
68t_off=$(chat off-turn2 "$(turn2_msgs "$a1_off")")
69health off >/dev/null
70stop_server
71
72echo "== on: opt in with a store dir that does not exist yet"
73D="$work/store"
74start_server on "$D"
75a1=$(chat on-turn1 "$m1")
76wait_parks
77t_on=$(chat on-turn2 "$(turn2_msgs "$a1")")
78wait_parks
79health on >/dev/null
80find "$D" -printf '%M %u %s %P\n' | sort -k4 >"$dir/on-perms.txt"
81on_files=$(find "$D" -type f | wc -l)
82on_perm_bad=$(perm_bad "$D")
83
84echo "== sigterm: SIGTERM while a streamed chat is generating"
85jq -n --arg model "$SERVED_MODEL" --argjson m "$(turn2_msgs "$a1" "List ten uses of a torque wrench, one line each, with a reason for each.")" \
86 '{model:$model, messages:$m, max_tokens:256, temperature:0, seed:1, stream:true}' >"$work/long.body"
87curl -sN -m 600 "$base/v1/chat/completions" -H "Authorization: Bearer $AIEN_SERVE_HTTP_BEARER_TOKEN" -H 'content-type: application/json' -d @"$work/long.body" >"$dir/sigterm-stream.sse" 2>&1 &
88cpid=$!
89for _ in $(seq 1 600); do
90 grep -q '"content":"[^"]' "$dir/sigterm-stream.sse" 2>/dev/null && break
91 sleep 0.2
92done
93spid=$(server_pid)
94t0=$SECONDS
95kill -TERM "$spid"
96wait "$cpid" || true
97rc=0
98wait "$SERVER_JOB" || rc=$?
99term_s=$((SECONDS - t0))
100rm -f "$PIDF"
101sig_done=$(grep -c 'data: \[DONE\]' "$dir/sigterm-stream.sse" || true)
102sig_tokens=$(grep -o '"completion_tokens":[0-9]*' "$dir/sigterm-stream.sse" | tail -1 | cut -d: -f2)
103sig_tmp=$(find "$D/tmp" -type f 2>/dev/null | wc -l)
104
105echo "== restart: same store"
106start_server restart "$D"
107t_restart=$(chat restart-turn2 "$(turn2_msgs "$a1")")
108health restart >/dev/null
109stop_server
110
111echo "== corrupt: flip a byte in every object, add a foreign file and a stray temp"
112find "$D/objects" -type f | while read -r f; do flip_byte "$f"; done
113head -c 4096 /dev/urandom >"$D/objects/zz-foreign.bin"
114head -c 4096 /dev/urandom >"$D/tmp/zz-stray.tmp"
115chmod 600 "$D/objects/zz-foreign.bin" "$D/tmp/zz-stray.tmp"
116start_server corrupt "$D"
117t_corrupt=$(chat corrupt-turn2 "$(turn2_msgs "$a1")")
118health corrupt >/dev/null
119stop_server
120corrupt_stray=$([ -e "$D/tmp/zz-stray.tmp" ] && echo present || echo removed)
121
122echo "== foreign: another valid key on the same store"
123start_server foreign "foreignkey:$D"
124t_foreign=$(chat foreign-turn2 "$(turn2_msgs "$a1")")
125health foreign >/dev/null
126stop_server
127
128echo "== unsafe: store dir with mode 0755"
129U="$work/unsafe"
130mkdir -p "$U"
131chmod 755 "$U"
132start_server unsafe "$U"
133t_unsafe=$(chat unsafe-turn2 "$(turn2_msgs "$a1")")
134health unsafe >/dev/null
135stop_server
136unsafe_entries=$(find "$U" -mindepth 1 | wc -l)
137
138echo "== nokey: AIEN_KV_STORE_DIR set, no AIEN_KV_STORE_KEY"
139start_server nokey "nokey:$work/store-nokey"
140t_nokey=$(chat nokey-turn2 "$(turn2_msgs "$a1")")
141health nokey >/dev/null
142stop_server
143
144echo "== rollback: unset AIEN_KV_STORE_DIR and restart"
145tree_sums "$D" >"$dir/rollback-store-before.sha256"
146start_server rollback -
147t_rollback=$(chat rollback-turn2 "$(turn2_msgs "$a1")")
148health rollback >/dev/null
149stop_server
150tree_sums "$D" >"$dir/rollback-store-after.sha256"
151
152{
153 check "off: no persistent_kv in health" "$(jq -r 'has("persistent_kv")' "$dir/off-health.json")" false
154 check "off: no persistent KV log line" "$(grep -c 'persistent KV' "$dir/off.log" || true)" 0
155 check "on: turn 1 == off" "$a1" "$a1_off"
156 check "on: T == T_off" "$t_on" "$t_off"
157 check "on: state on, store open" "$(pk on state)/$(pk on store_state)" on/open
158 check "on: recovery done" "$(pk on recovery.done)" true
159 for k in parked_chunks park_failed restores restore_misses refusals seal recovery io; do
160 check "on: health has $k" "$(jq -r ".persistent_kv | has(\"$k\")" "$dir/on-health.json")" true
161 done
162 check "on: no park failed" "$(pk on park_failed)" 0
163 check "on: the store holds files" "$([ "$on_files" -gt 0 ] && echo yes || echo "no ($on_files)")" yes
164 check "on: every dir 0700, every file 0600" "$on_perm_bad" 0
165 check "sigterm: the in-flight chat finished ([DONE])" "$sig_done" 1
166 check "sigterm: the in-flight chat got tokens" "$([ "${sig_tokens:-0}" -gt 0 ] && echo yes || echo no)" yes
167 check "sigterm: exit status 0" "$rc" 0
168 check "sigterm: exited within ${PKV_STOP_WAIT_S:-120} s" "$([ "$term_s" -le "${PKV_STOP_WAIT_S:-120}" ] && echo yes || echo "no (${term_s} s)")" yes
169 check "sigterm: log says shutdown complete" "$(grep -c 'shutdown complete' "$dir/on.log" || true)" 1
170 check "sigterm: no temp file left" "$sig_tmp" 0
171 check "restart: restores once" "$(pk restart restores)" 1
172 check "restart: T == T_off" "$t_restart" "$t_off"
173 check "restart: recovery indexed objects" "$(jq -r '.persistent_kv.recovery.indexed > 0' "$dir/restart-health.json")" true
174 check "corrupt: serves, store not failed" "$(jq -r '.persistent_kv.store_state != "failed"' "$dir/corrupt-health.json")" true
175 check "corrupt: restores nothing" "$(pk corrupt restores)" 0
176 check "corrupt: T == T_off" "$t_corrupt" "$t_off"
177 check "corrupt: recovery removed the stray temp" "$corrupt_stray" removed
178 check "corrupt: health counts the removed temp" "$(jq -r '.persistent_kv.recovery.tmp_deleted >= 1' "$dir/corrupt-health.json")" true
179 check "foreign: restores nothing" "$(pk foreign restores)" 0
180 check "foreign: T == T_off" "$t_foreign" "$t_off"
181 check "unsafe: refused at startup" "$(pk unsafe state)/$(pk unsafe refusals)" refused/1
182 check "unsafe: T == T_off" "$t_unsafe" "$t_off"
183 check "unsafe: the dir stays empty" "$unsafe_entries" 0
184 check "nokey: refused at startup" "$(pk nokey state)/$(pk nokey refusals)" refused/1
185 check "nokey: T == T_off" "$t_nokey" "$t_off"
186 check "rollback: no persistent_kv in health" "$(jq -r 'has("persistent_kv")' "$dir/rollback-health.json")" false
187 check "rollback: no persistent KV log line" "$(grep -c 'persistent KV' "$dir/rollback.log" || true)" 0
188 check "rollback: T == T_off" "$t_rollback" "$t_off"
189 check "rollback: the store is untouched" "$(cmp -s "$dir/rollback-store-before.sha256" "$dir/rollback-store-after.sha256" && echo same || echo changed)" same
190 echo "recovery after corrupt: $(jq -c '.persistent_kv.recovery' "$dir/corrupt-health.json")"
191 echo "misses after corrupt: $(jq -c '.persistent_kv.restore_misses' "$dir/corrupt-health.json"); after foreign: $(jq -c '.persistent_kv.restore_misses' "$dir/foreign-health.json")"
192 echo "unsafe reason: $(pk unsafe reason); nokey reason: $(pk nokey reason)"
193 [ $fail = 0 ] && echo "PKV OPS CHECK: PASS" || echo "PKV OPS CHECK: FAIL"
194} | tee "$dir/summary.txt"
195grep -q "PKV OPS CHECK: PASS" "$dir/summary.txt" || fail=1
196pkv_seal
197exit $fail