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 / tools / qwen38_serve_gate.sh
| 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 | # Correctness gate for the live Qwen3.8-27B server (sc #642): greedy ids through the real HTTP |
| 18 | # door against the host reference golden, a negative control that must fail, and next-turn resume |
| 19 | # (snapshot and live) against cold recomputation. |
| 20 | # Usage: qwen38_serve_gate.sh [base-url] [golden.json] <out-dir> [server-log] |
| 21 | # base-url default http://127.0.0.1:18091 |
| 22 | # golden.json default: the host reference shipped in this repository |
| 23 | # (crates/aien-inference-abi/fixtures/qwen38/gen-chat-max-24.json); "" also means default |
| 24 | # server-log default $AIEN_SERVE_LOG, else the log tools/serve-27b.sh writes: |
| 25 | # ${XDG_STATE_HOME:-$HOME/.local/state}/aien/serve-27b.log |
| 26 | # Writes <out-dir>/summary.txt (names the server binary sha256 and model folder it tested) and |
| 27 | # <out-dir>/receipt.json (build, binary sha256, model checksum list, machine state, per-request |
| 28 | # figures, MATCH or MISMATCH against the shipped golden). See tools/qwen38_receipt.sh for how the |
| 29 | # server is found (SERVER_BIN and MODEL_DIR override). Needs curl, jq, sha256sum and awk. |
| 30 | set -uo pipefail |
| 31 | REPO=$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd) |
| 32 | URL=${1:-http://127.0.0.1:18091} |
| 33 | GOLDEN=${2:-$REPO/crates/aien-inference-abi/fixtures/qwen38/gen-chat-max-24.json} |
| 34 | OUT=${3:?usage: qwen38_serve_gate.sh [base-url] [golden.json] <out-dir> [server-log]} |
| 35 | LOG=${4:-${AIEN_SERVE_LOG:-${XDG_STATE_HOME:-$HOME/.local/state}/aien/serve-27b.log}} |
| 36 | for t in curl jq; do command -v "$t" >/dev/null || { echo "qwen38_serve_gate.sh: needs $t" >&2; exit 2; }; done |
| 37 | [ -n "${AIEN_SERVE_HTTP_BEARER_TOKEN:-}" ] || { echo "qwen38_serve_gate.sh: AIEN_SERVE_HTTP_BEARER_TOKEN required" >&2; exit 2; } |
| 38 | [ -f "$GOLDEN" ] || { echo "qwen38_serve_gate.sh: golden file not found: $GOLDEN" >&2; exit 2; } |
| 39 | [ -f "$LOG" ] || { echo "qwen38_serve_gate.sh: server log not found: $LOG (pass it as the 4th argument or set AIEN_SERVE_LOG)" >&2; exit 2; } |
| 40 | mkdir -p "$OUT" |
| 41 | # shellcheck source=tools/qwen38_receipt.sh |
| 42 | . "$REPO/tools/qwen38_receipt.sh" |
| 43 | q38_server_facts "$URL" |
| 44 | # shellcheck disable=SC2034 # read by q38_write_receipt |
| 45 | Q38_MACHINE_START=$(q38_machine_json) |
| 46 | : > "$OUT/runs.jsonl" |
| 47 | N=$(jq '.generated_ids | length' "$GOLDEN") |
| 48 | WANT=$(jq -c '.generated_ids' "$GOLDEN") |
| 49 | WANT_PROMPT=$(jq '.prompt_ids | length' "$GOLDEN") |
| 50 | Q1="Write a C function that returns the larger of two ints." |
| 51 | Q1X="Write a C function that returns the smaller of two ints." |
| 52 | Q2="Now make it work for three ints." |
| 53 | Q3="Thanks." |
| 54 | # ask <name> <messages-json>: greedy, thinking off, ids returned; saves the reply as <name>.json |
| 55 | ask() { |
| 56 | jq -n --argjson m "$2" --argjson n "$N" \ |
| 57 | '{messages:$m, temperature:0, max_tokens:$n, chat_template_kwargs:{enable_thinking:false}, aien_return_token_ids:true}' \ |
| 58 | > "$OUT/$1.req.json" |
| 59 | # Other clients share the server: retry while it answers busy (one generation at a time). |
| 60 | local w="" |
| 61 | for _ in $(seq 1 240); do |
| 62 | w=$(curl -s -m 900 -o "$OUT/$1.json" -w "%{time_total}" "$URL/v1/chat/completions" -H "Authorization: Bearer $AIEN_SERVE_HTTP_BEARER_TOKEN" -H 'Content-Type: application/json' -d @"$OUT/$1.req.json") |
| 63 | # Retry only "busy" answers (server_busy, chip lock); any other error is the result. |
| 64 | jq -e '.error | tostring | test("busy"; "i")' "$OUT/$1.json" >/dev/null 2>&1 || break |
| 65 | sleep 5 |
| 66 | done |
| 67 | q38_run_json "$1" "$OUT/$1.json" "$w" "$LOG" >> "$OUT/runs.jsonl" |
| 68 | } |
| 69 | # The server's own record of how a reply's prompt was fed (the qwen38 line before its id). |
| 70 | reuse() { grep -B1 "$(jq -r '.id' "$OUT/$1.json"):" "$LOG" | grep -o 'reuse [A-Za-z]*([0-9]*)\|reuse Cold' | head -1; } |
| 71 | ids() { jq -c '.usage.aien_token_ids' "$OUT/$1.json"; } |
| 72 | content() { jq -r '.choices[0].message.content' "$OUT/$1.json"; } |
| 73 | m1=$(jq -n --arg q "$Q1" '[{role:"user",content:$q}]') |
| 74 | ask golden "$m1" |
| 75 | G_IDS=$(ids golden); G_PT=$(jq '.usage.prompt_tokens' "$OUT/golden.json") |
| 76 | C1=$(content golden) |
| 77 | # Turn 2b edits the reply. A short cold prompt has no checkpoint under the Omega |
| 78 | # pass-budget policy, so this must recompute cold and keep identical answers. |
| 79 | m2b=$(jq -n --arg q "$Q1" --arg q2 "$Q2" '[{role:"user",content:$q},{role:"assistant",content:"Sure."},{role:"user",content:$q2}]') |
| 80 | ask turn2b_resumed "$m2b" |
| 81 | # Turn 1 again (cold: nothing held matches) so turn 2 below continues from it. |
| 82 | ask golden2 "$m1" |
| 83 | m2=$(jq -n --arg q "$Q1" --arg a "$C1" --arg q2 "$Q2" '[{role:"user",content:$q},{role:"assistant",content:$a},{role:"user",content:$q2}]') |
| 84 | ask turn2_resumed "$m2" |
| 85 | C2=$(content turn2_resumed) |
| 86 | m3=$(jq -n --arg q "$Q1" --arg a "$C1" --arg q2 "$Q2" --arg a2 "$C2" --arg q3 "$Q3" \ |
| 87 | '[{role:"user",content:$q},{role:"assistant",content:$a},{role:"user",content:$q2},{role:"assistant",content:$a2},{role:"user",content:$q3}]') |
| 88 | ask turn3_resumed "$m3" |
| 89 | # Negative control: a different question must not give the golden ids. It also leaves no state |
| 90 | # that the turn 2 / turn 3 prompts can reuse, so the next two requests run cold. |
| 91 | ask control "$(jq -n --arg q "$Q1X" '[{role:"user",content:$q}]')" |
| 92 | ask turn2_cold "$m2" |
| 93 | ask turn2b_cold "$m2b" |
| 94 | ask control2 "$(jq -n --arg q "$Q1X" '[{role:"user",content:$q}]')" |
| 95 | ask turn3_cold "$m3" |
| 96 | # Long prompts must still exercise real snapshot reuse. This prefix is larger than |
| 97 | # the default 4096-token chunk; assert that from the actual model token count below. |
| 98 | LONG_Q=$(awk 'BEGIN { for (i=0; i<2500; i++) printf "orchard "; print "\nReply only: ready" }') |
| 99 | ml=$(jq -n --arg q "$LONG_Q" '[{role:"user",content:$q}]') |
| 100 | ml2=$(jq -n --arg q "$LONG_Q" '[{role:"user",content:$q},{role:"assistant",content:"Sure."},{role:"user",content:"Reply only: ready"}]') |
| 101 | ask long_first "$ml" |
| 102 | ask long_edited_resumed "$ml2" |
| 103 | ask long_reset "$m1" |
| 104 | ask long_edited_cold "$ml2" |
| 105 | # A -> unrelated short B -> A catches a stale snapshot whose attention rows B |
| 106 | # overwrote. B must clear the snapshot even though B creates no replacement. |
| 107 | ask aba_a "$ml" |
| 108 | ask aba_b "$m1" |
| 109 | ask aba_a_again "$ml" |
| 110 | pass=1 |
| 111 | check() { if [ "$2" = 1 ]; then echo "PASS $1"; else echo "FAIL $1"; pass=0; fi; } |
| 112 | { |
| 113 | echo "server $URL pid $Q38_PID" |
| 114 | echo "server binary sha256 $Q38_BIN_SHA256 (${Q38_BIN:-unknown})" |
| 115 | echo "server build $Q38_BUILD" |
| 116 | echo "model dir $Q38_MODEL_DIR" |
| 117 | echo "golden $GOLDEN sha256 $(sha256sum "$GOLDEN" | cut -d" " -f1)" |
| 118 | # A busy chip (503) voids the run instead of failing it silently as null ids. |
| 119 | for f in "$OUT"/*.json; do case $f in *.req.json) continue;; esac |
| 120 | jq -e '.usage.aien_token_ids' "$f" >/dev/null || echo "VOID $(basename "$f"): $(head -c 200 "$f")"; done |
| 121 | echo "golden ids want $WANT" |
| 122 | echo "golden ids got $G_IDS" |
| 123 | check "golden: $N greedy ids equal the host reference" "$([ "$G_IDS" = "$WANT" ] && echo 1)" |
| 124 | check "golden: prompt is the reference's $WANT_PROMPT tokens" "$([ "$G_PT" = "$WANT_PROMPT" ] && echo 1)" |
| 125 | check "golden repeat: same ids" "$([ "$(ids golden2)" = "$WANT" ] && echo 1)" |
| 126 | check "control (smaller): ids differ from the golden (must)" "$([ "$(ids control)" != "$WANT" ] && [ "$(ids control)" != null ] && echo 1)" |
| 127 | echo "turn2 resumed $(ids turn2_resumed)"; echo "turn2 cold $(ids turn2_cold)" |
| 128 | for n in golden turn2b_resumed turn2_resumed turn3_resumed control turn2_cold turn2b_cold turn3_cold; do echo "$n: $(reuse $n)"; done |
| 129 | check "short edited reply recomputed cold without an eager snapshot" "$([ "$(reuse turn2b_resumed)" = "reuse Cold" ] && echo 1)" |
| 130 | check "turn 2 resumed request reused a state" "$(reuse turn2_resumed | grep -q 'Live\|Snapshot' && echo 1)" |
| 131 | echo "turn2b resumed $(ids turn2b_resumed)"; echo "turn2b cold $(ids turn2b_cold)" |
| 132 | check "turn 2b edited reply == cold" "$([ "$(ids turn2b_resumed)" = "$(ids turn2b_cold)" ] && [ "$(ids turn2b_cold)" != null ] && echo 1)" |
| 133 | check "turn 3 resumed request reused a state" "$(reuse turn3_resumed | grep -q 'Live\|Snapshot' && echo 1)" |
| 134 | check "cold requests ran cold" "$([ "$(reuse turn2_cold)" = "reuse Cold" ] && [ "$(reuse turn2b_cold)" = "reuse Cold" ] && [ "$(reuse turn3_cold)" = "reuse Cold" ] && echo 1)" |
| 135 | check "turn 2 resumed == cold" "$([ "$(ids turn2_resumed)" = "$(ids turn2_cold)" ] && [ "$(ids turn2_cold)" != null ] && echo 1)" |
| 136 | echo "turn3 resumed $(ids turn3_resumed)"; echo "turn3 cold $(ids turn3_cold)" |
| 137 | check "turn 3 resumed == cold" "$([ "$(ids turn3_resumed)" = "$(ids turn3_cold)" ] && [ "$(ids turn3_cold)" != null ] && echo 1)" |
| 138 | check "turn 2 differs from turn 1 (no stale reply)" "$([ "$(ids turn2_cold)" != "$G_IDS" ] && echo 1)" |
| 139 | check "long fixture exceeds the default chunk" "$(jq -e '.usage.prompt_tokens > 4096' "$OUT/long_first.json" >/dev/null && echo 1)" |
| 140 | check "long edited reply reused a snapshot" "$(reuse long_edited_resumed | grep -q Snapshot && echo 1)" |
| 141 | check "long edited cold comparison actually ran cold" "$([ "$(reuse long_edited_cold)" = "reuse Cold" ] && echo 1)" |
| 142 | check "long snapshot reply == cold" "$([ "$(ids long_edited_resumed)" = "$(ids long_edited_cold)" ] && [ "$(ids long_edited_cold)" != null ] && echo 1)" |
| 143 | check "unrelated short B invalidated A snapshot" "$([ "$(reuse aba_b)" = "reuse Cold" ] && [ "$(reuse aba_a_again)" = "reuse Cold" ] && echo 1)" |
| 144 | check "A after unrelated B == A before B" "$([ "$(ids aba_a)" = "$(ids aba_a_again)" ] && [ "$(ids aba_a_again)" != null ] && echo 1)" |
| 145 | echo "golden text: $C1" |
| 146 | [ $pass = 1 ] && echo "GATE PASS" || echo "GATE FAIL" |
| 147 | } > "$OUT/summary.txt" |
| 148 | G_MATCH=MISMATCH; [ "$G_IDS" = "$WANT" ] && G_MATCH=MATCH |
| 149 | echo "golden ids against the shipped reference: $G_MATCH" >> "$OUT/summary.txt" |
| 150 | VERDICT=$(grep -q '^GATE PASS$' "$OUT/summary.txt" && echo PASS || echo FAIL) |
| 151 | EXTRA=$(jq -n --arg golden "$GOLDEN" --arg gsha "$(sha256sum "$GOLDEN" | cut -d' ' -f1)" \ |
| 152 | --arg m "$G_MATCH" --arg v "$VERDICT" --rawfile s "$OUT/summary.txt" \ |
| 153 | '{reference:{file:$golden, sha256:$gsha}, golden_ids:$m, verdict:$v, summary:($s | rtrimstr("\n") | split("\n"))}') |
| 154 | q38_write_receipt "$OUT/receipt.json" gate "$URL" "$OUT/runs.jsonl" "$EXTRA" |
| 155 | cat "$OUT/summary.txt" |
| 156 | echo "receipt: $OUT/receipt.json" |
| 157 | [ "$VERDICT" = PASS ] |