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
aien-sovereign-core-mv / scripts / own30b-pkv-ops-check.sh
| 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. |
| 45 | set -euo pipefail |
| 46 | . "$(dirname "$0")/own30b-pkv-lib.sh" |
| 47 | pkv_usage_args "$(basename "$0")" "$@" |
| 48 | |
| 49 | if [ "$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 |
| 57 | fi |
| 58 | pkv_begin |
| 59 | |
| 60 | perm_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 | } |
| 63 | tree_sums() { (cd "$1" && find . -type f -print0 | sort -z | xargs -0 -r sha256sum); } |
| 64 | |
| 65 | echo "== off: AIEN_KV_STORE_DIR unset" |
| 66 | start_server off - |
| 67 | a1_off=$(chat off-turn1 "$m1") |
| 68 | t_off=$(chat off-turn2 "$(turn2_msgs "$a1_off")") |
| 69 | health off >/dev/null |
| 70 | stop_server |
| 71 | |
| 72 | echo "== on: opt in with a store dir that does not exist yet" |
| 73 | D="$work/store" |
| 74 | start_server on "$D" |
| 75 | a1=$(chat on-turn1 "$m1") |
| 76 | wait_parks |
| 77 | t_on=$(chat on-turn2 "$(turn2_msgs "$a1")") |
| 78 | wait_parks |
| 79 | health on >/dev/null |
| 80 | find "$D" -printf '%M %u %s %P\n' | sort -k4 >"$dir/on-perms.txt" |
| 81 | on_files=$(find "$D" -type f | wc -l) |
| 82 | on_perm_bad=$(perm_bad "$D") |
| 83 | |
| 84 | echo "== sigterm: SIGTERM while a streamed chat is generating" |
| 85 | jq -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" |
| 87 | curl -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 & |
| 88 | cpid=$! |
| 89 | for _ in $(seq 1 600); do |
| 90 | grep -q '"content":"[^"]' "$dir/sigterm-stream.sse" 2>/dev/null && break |
| 91 | sleep 0.2 |
| 92 | done |
| 93 | spid=$(server_pid) |
| 94 | t0=$SECONDS |
| 95 | kill -TERM "$spid" |
| 96 | wait "$cpid" || true |
| 97 | rc=0 |
| 98 | wait "$SERVER_JOB" || rc=$? |
| 99 | term_s=$((SECONDS - t0)) |
| 100 | rm -f "$PIDF" |
| 101 | sig_done=$(grep -c 'data: \[DONE\]' "$dir/sigterm-stream.sse" || true) |
| 102 | sig_tokens=$(grep -o '"completion_tokens":[0-9]*' "$dir/sigterm-stream.sse" | tail -1 | cut -d: -f2) |
| 103 | sig_tmp=$(find "$D/tmp" -type f 2>/dev/null | wc -l) |
| 104 | |
| 105 | echo "== restart: same store" |
| 106 | start_server restart "$D" |
| 107 | t_restart=$(chat restart-turn2 "$(turn2_msgs "$a1")") |
| 108 | health restart >/dev/null |
| 109 | stop_server |
| 110 | |
| 111 | echo "== corrupt: flip a byte in every object, add a foreign file and a stray temp" |
| 112 | find "$D/objects" -type f | while read -r f; do flip_byte "$f"; done |
| 113 | head -c 4096 /dev/urandom >"$D/objects/zz-foreign.bin" |
| 114 | head -c 4096 /dev/urandom >"$D/tmp/zz-stray.tmp" |
| 115 | chmod 600 "$D/objects/zz-foreign.bin" "$D/tmp/zz-stray.tmp" |
| 116 | start_server corrupt "$D" |
| 117 | t_corrupt=$(chat corrupt-turn2 "$(turn2_msgs "$a1")") |
| 118 | health corrupt >/dev/null |
| 119 | stop_server |
| 120 | corrupt_stray=$([ -e "$D/tmp/zz-stray.tmp" ] && echo present || echo removed) |
| 121 | |
| 122 | echo "== foreign: another valid key on the same store" |
| 123 | start_server foreign "foreignkey:$D" |
| 124 | t_foreign=$(chat foreign-turn2 "$(turn2_msgs "$a1")") |
| 125 | health foreign >/dev/null |
| 126 | stop_server |
| 127 | |
| 128 | echo "== unsafe: store dir with mode 0755" |
| 129 | U="$work/unsafe" |
| 130 | mkdir -p "$U" |
| 131 | chmod 755 "$U" |
| 132 | start_server unsafe "$U" |
| 133 | t_unsafe=$(chat unsafe-turn2 "$(turn2_msgs "$a1")") |
| 134 | health unsafe >/dev/null |
| 135 | stop_server |
| 136 | unsafe_entries=$(find "$U" -mindepth 1 | wc -l) |
| 137 | |
| 138 | echo "== nokey: AIEN_KV_STORE_DIR set, no AIEN_KV_STORE_KEY" |
| 139 | start_server nokey "nokey:$work/store-nokey" |
| 140 | t_nokey=$(chat nokey-turn2 "$(turn2_msgs "$a1")") |
| 141 | health nokey >/dev/null |
| 142 | stop_server |
| 143 | |
| 144 | echo "== rollback: unset AIEN_KV_STORE_DIR and restart" |
| 145 | tree_sums "$D" >"$dir/rollback-store-before.sha256" |
| 146 | start_server rollback - |
| 147 | t_rollback=$(chat rollback-turn2 "$(turn2_msgs "$a1")") |
| 148 | health rollback >/dev/null |
| 149 | stop_server |
| 150 | tree_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" |
| 195 | grep -q "PKV OPS CHECK: PASS" "$dir/summary.txt" || fail=1 |
| 196 | pkv_seal |
| 197 | exit $fail |