okami.mom/strata

git clone https://git.nkp.pet/okami.mom/strata

Niko1221rc1 merge tui-update (3165c2e8a)a1cc285fa

main
13.4 KiB204 linesraw
1#!/usr/bin/env bash
2# An engine update beside a LOADED MODEL, from the BUILT Terminal package (not the checkout), real engines, a real model, a throwaway HOME.
3#
4#   TERMINAL_ARCHIVE=<built strata-terminal-*.tar.gz> REL28=<dir> REL29=<dir> MODEL=<qwen model folder> bash tests/terminal/update_model_flow.sh [A|B]
5#
6#   REL28 / REL29  the real, signed release files (read only): strata-feed.json(.sig) and strata-engine-<ver>-linux-x86_64-cuda-cuda13.tar.gz of v0.2.8
7#                  (engine 0.1.54) and v0.2.9 (engine 0.1.55); each feed is served from 127.0.0.1 (the box needs no internet).
8#   MODEL          a Qwen model folder the engine can load (the flow passes it as --model-path).
9#   DATA           (optional) a data folder that already holds what setup would download (tokenizer, picture reader, draft layer, the prepared pack):
10#                  a box without internet cannot fetch them. Default: a new folder inside $W.
11#   stage A        0.1.54 installed, the model loaded and answering; the notice; a script is refused; "no"; a failed download; "yes" + start again
12#                  on 0.1.55 and a real answer from it.
13#   stage B        (needs stage A's HOME) back on 0.1.54: "yes" + "no" to start again, started by hand; then an engine that passes its checks but
14#                  does not start: the model is started again on the old engine and answers.
15# Nothing touches the real HOME: HOME, XDG_* and the settings file are inside $W, which is removed at the end unless KEEP=1.
16set -uo pipefail
17HERE="$(cd "$(dirname "$0")" && pwd)"
18REPO="$(cd "$HERE/../.." && pwd)"
19W="${FLOW_DIR:-$HOME/update-model-flow}"
20STAGE="${1:-A}"
21FAIL=0
22say() { printf '\n=== %s  (%s)\n' "$*" "$(date +%H:%M:%S)"; }
23ok() { printf 'PASS  %s\n' "$*"; }
24bad() { printf 'FAIL  %s\n' "$*"; FAIL=1; }
25check() { if eval "$2"; then ok "$1"; else bad "$1  [$2]"; fi; }
26PIDS=()
27cleanup() {
28  [ -x "${S:-/nonexistent}" ] && "$S" launch --stop >/dev/null 2>&1
29  for p in "${PIDS[@]:-}"; do [ -n "$p" ] && kill "$p" 2>/dev/null; done
30  [ "${KEEP:-1}" != 1 ] && [ "$STAGE" = B ] && rm -rf "$W"
31}
32trap cleanup EXIT
33: "${TERMINAL_ARCHIVE:?}" "${REL28:?}" "${REL29:?}" "${MODEL:?}"
34mkdir -p "$W"/{term,home,logs}
35if [ ! -x "$(find "$W/term" -maxdepth 3 -path '*/bin/strata' 2>/dev/null | head -1)" ]; then tar -xzf "$TERMINAL_ARCHIVE" -C "$W/term"; fi
36S="$(find "$W/term" -maxdepth 3 -path '*/bin/strata' | head -1)"
37export HOME="$W/home" XDG_DATA_HOME="$W/home/.local/share" XDG_CONFIG_HOME="$W/home/.config" XDG_STATE_HOME="$W/home/.local/state" XDG_CACHE_HOME="$W/home/.cache"
38export STRATA_SETTINGS_FILE="$W/home/app-settings.json"
39unset STRATA_NO_ACQUIRE STRATA_COMPONENTS_DIR STRATA_API_KEY STRATA_UPDATE_FEED STRATA_ENGINE_BACKEND STRATA_INSTALL_DIR STRATA_HOME STRATA_UPDATE_TRUSTED_KEYS
40COMP="$XDG_DATA_HOME/strata/components"
41PORT_MODEL=18480
42ENG54=strata-engine-0.1.54-linux-x86_64-cuda-cuda13.tar.gz
43ENG55=strata-engine-0.1.55-linux-x86_64-cuda-cuda13.tar.gz
44echo "package: $(basename "$TERMINAL_ARCHIVE")  launcher: $S"
45check "the package has its launcher" "[ -x '$S' ]"
46
47serve() {          # serve DIR on a free port; sets PORT and SERVER_PID (no subshell: the pid survives)
48  local dir="$1" log="$2" port; shift 2
49  port=$(python3 -c 'import socket;s=socket.socket();s.bind(("127.0.0.1",0));print(s.getsockname()[1])')
50  python3 "$HERE/cutserver.py" "$port" "$dir" "$@" > "$log" 2>&1 &
51  SERVER_PID=$!; PIDS+=("$SERVER_PID")
52  for _ in $(seq 1 50); do curl -fs -o /dev/null "http://127.0.0.1:$port/strata-feed.json" && break; sleep 0.2; done
53  PORT="$port"
54}
55json() { python3 -c "import json,sys; d=json.load(sys.stdin); print($1)"; }
56engine_in_use() {  # the engine FOLDER the loaded model's engine process runs from (read from /proc, not from our own bookkeeping)
57  local p exe
58  for p in /proc/[0-9]*; do
59    exe=$(readlink "$p/exe" 2>/dev/null) || continue
60    case "$exe" in "$COMP"/engine/*) echo "$exe" | sed "s#.*/engine/\([^/]*\)/.*#\1#"; return;; esac
61  done
62}
63engine_pid() { local p exe; for p in /proc/[0-9]*; do exe=$(readlink "$p/exe" 2>/dev/null) || continue; case "$exe" in "$COMP"/engine/*) echo "${p#/proc/}"; return;; esac; done; }
64active_version() { "$S" components engine --output json --backend cuda | json "(d.get('engine') or {}).get('version')"; }
65answers() { python3 "$HERE/prompt_check.py" "$PORT_MODEL"; }
66launch_model() {   # strata launch with the same choices every time (the 'same settings' the update must bring back)
67  "$S" launch --family qwen --model iq3_s --model-path "$MODEL" --data-dir "${DATA:-$W/home/data}" --port "$PORT_MODEL" --no-api-key --context 8k --yes --no-tui --feed "$1" 2>&1 | tail -12
68  return "${PIPESTATUS[0]}"
69}
70engine_gets() { grep -c "GET /$1" "$2" || true; }
71
72if [ "$STAGE" = A ]; then
73  say "stage A: 0.1.54 and a loaded model"
74  serve "$REL28" "$W/logs/server28.log"; P28=$PORT; PID28=$SERVER_PID
75  F28="http://127.0.0.1:$P28/strata-feed.json"
76  "$S" update --feed "$F28" --no-control --backend cuda 2>&1 | tail -4
77  check "0.1.54 is the engine in use" "[ \"\$(active_version)\" = 0.1.54 ]"
78  launch_model "$F28"; RC=$?
79  check "the model loaded (launch exit 0)" "[ $RC -eq 0 ]"
80  check "its engine process runs from the 0.1.54 folder" "[ \"\$(engine_in_use | cut -d- -f1)\" = 0.1.54 ]"
81  check "it answers a real question on 0.1.54" "answers"
82  OLDPID=$(engine_pid); echo "engine pid on 0.1.54: $OLDPID"
83  kill "$PID28"; wait "$PID28" 2>/dev/null
84
85  serve "$REL29" "$W/logs/server29.log"; P29=$PORT
86  F29="http://127.0.0.1:$P29/strata-feed.json"
87
88  say "A1: the check shows the notice and the one next step, and names the running model"
89  "$S" update --check --feed "$F29" --no-control --backend cuda 2>&1 | tee "$W/logs/check.txt"
90  check "update --check says an update is available" "grep -q 'update available: 0.1.54 -> 0.1.55' '$W/logs/check.txt'"
91  check "it names the running model and says it will ask first" "grep -q 'is running. An update asks before it stops it' '$W/logs/check.txt'"
92  check "a check downloaded no engine" "[ \"\$(engine_gets $ENG55 '$W/logs/server29.log')\" = 0 ]"
93
94  say "A2: the launch start screen shows one quiet line"
95  python3 "$HERE/pty_drive.py" --timeout 40 --quit-after 12 --quit-with q -- "$S" launch --feed "$F29" > "$W/logs/start-screen.txt" 2>&1
96  check "the start screen carries the line" "sed 's/\x1b\[[0-9;?]*[A-Za-z]//g' '$W/logs/start-screen.txt' | tr -s ' \n' ' ' | grep -q 'A new engine 0.1.55 (you have 0.1.54) is available. Update with .strata update.'"
97  check "the model was not touched by looking at it" "[ \"\$(engine_pid)\" = '$OLDPID' ]"
98
99  say "A3: a script is refused (--yes alone never stops a model), nothing is downloaded"
100  "$S" update --feed "$F29" --no-control --backend cuda --yes; RC=$?
101  check "exit 7" "[ $RC -eq 7 ]"
102  check "no engine archive was requested" "[ \"\$(engine_gets $ENG55 '$W/logs/server29.log')\" = 0 ]"
103  check "the same engine process is still serving, still 0.1.54" "[ \"\$(engine_pid)\" = '$OLDPID' ] && [ \"\$(active_version)\" = 0.1.54 ]"
104  check "and still answers" "answers"
105
106  say "A4: 'no' at the question changes nothing"
107  python3 "$HERE/pty_drive.py" --timeout 300 --answer 'for the update\?.*=>n' -- "$S" update --feed "$F29" --no-control --backend cuda > "$W/logs/no.txt" 2>&1; RC=$?
108  cat "$W/logs/no.txt" | sed 's/\x1b\[[0-9;?]*[A-Za-z]//g' | tail -12
109  check "exit 0" "[ $RC -eq 0 ]"
110  check "it said nothing was changed" "grep -q 'Nothing was changed' '$W/logs/no.txt'"
111  check "the warning named the model" "sed 's/\x1b\[[0-9;?]*[A-Za-z]//g' '$W/logs/no.txt' | tr -s ' \n' ' ' | grep -qi 'is running'"
112  check "no engine archive requested, same engine process" "[ \"\$(engine_gets $ENG55 '$W/logs/server29.log')\" = 0 ] && [ \"\$(engine_pid)\" = '$OLDPID' ]"
113
114  say "A5: the download fails: the model is not stopped, the old engine stays"
115  serve "$REL29" "$W/logs/server29cut.log" "$ENG55" 50000000; PCUT=$PORT; PIDCUT=$SERVER_PID
116  "$S" update --feed "http://127.0.0.1:$PCUT/strata-feed.json" --no-control --backend cuda --stop-model --start-again; RC=$?
117  check "exit 6 (failed, the old engine is in use)" "[ $RC -eq 6 ]"
118  check "the model was never stopped (same engine process)" "[ \"\$(engine_pid)\" = '$OLDPID' ]"
119  check "0.1.54 is still the engine" "[ \"\$(active_version)\" = 0.1.54 ]"
120  check "and it still answers" "answers"
121  kill "$PIDCUT"; wait "$PIDCUT" 2>/dev/null
122
123  say "A6: 'yes', then 'yes' to start it again with the same settings"
124  python3 "$HERE/pty_drive.py" --timeout 1500 --answer 'for the update\?.*=>y' --answer 'again with the same settings\?.*=>y' -- "$S" update --feed "$F29" --no-control --backend cuda > "$W/logs/yes.txt" 2>&1; RC=$?
125  sed 's/\x1b\[[0-9;?]*[A-Za-z]//g' "$W/logs/yes.txt" | tr -d '\r' | grep -v '^$' | tail -25
126  check "exit 0" "[ $RC -eq 0 ]"
127  PLAIN="$(sed 's/\x1b\[[0-9;?]*[A-Za-z]//g' "$W/logs/yes.txt" | tr -d '\r' | tr -s ' \n' ' ')"
128  check "the warning named the model before anything happened" "echo \"\$PLAIN\" | grep -q 'is running'"
129  check "the engine was downloaded before the model was stopped" "python3 - <<'PY'
130import re,sys
131t=open('$W/logs/yes.txt',errors='replace').read()
132t=re.sub(r'\x1b\[[0-9;?]*[A-Za-z]','',t)
133a=t.find('downloading'); b=t.find('Stopping ')
134sys.exit(0 if 0<=a<b else 1)
135PY"
136  check "the old engine process is gone and a new one serves" "[ -n \"\$(engine_pid)\" ] && [ \"\$(engine_pid)\" != '$OLDPID' ]"
137  check "the new engine process runs from the 0.1.55 folder" "[ \"\$(engine_in_use | cut -d- -f1)\" = 0.1.55 ]"
138  check "0.1.55 is the engine in use and 0.1.54 is kept" "[ \"\$(active_version)\" = 0.1.55 ] && ls '$COMP'/engine | grep -q 0.1.54"
139  check "a real question is answered on the NEW engine" "answers"
140  "$S" launch --stop 2>&1 | tail -1
141  kill "$SERVER_PID" 2>/dev/null
142fi
143
144if [ "$STAGE" = B ]; then
145  say "stage B: back on 0.1.54 with the model loaded"
146  "$S" launch --stop 2>&1 | tail -1
147  "$S" components use 0.1.54 2>&1 | tail -2
148  check "0.1.54 is in use" "[ \"\$(active_version)\" = 0.1.54 ]"
149  serve "$REL29" "$W/logs/server29b.log"; P29=$PORT; F29="http://127.0.0.1:$P29/strata-feed.json"
150  launch_model "$F29"; RC=$?
151  check "the model loaded on 0.1.54" "[ $RC -eq 0 ] && [ \"\$(engine_in_use | cut -d- -f1)\" = 0.1.54 ]"
152
153  say "B1: 'yes', then 'no' to start it again: it stays stopped and the way to start it is printed"
154  python3 "$HERE/pty_drive.py" --timeout 1500 --answer 'for the update\?.*=>y' --answer 'again with the same settings\?.*=>n' -- "$S" update --feed "$F29" --no-control --backend cuda > "$W/logs/yes-no.txt" 2>&1; RC=$?
155  sed 's/\x1b\[[0-9;?]*[A-Za-z]//g' "$W/logs/yes-no.txt" | tr -d '\r' | grep -v '^$' | tail -14
156  check "exit 0" "[ $RC -eq 0 ]"
157  check "no engine process is running" "[ -z \"\$(engine_pid)\" ]"
158  check "0.1.55 is in use" "[ \"\$(active_version)\" = 0.1.55 ]"
159  check "it printed how to start the model by hand" "sed 's/\x1b\[[0-9;?]*[A-Za-z]//g' '$W/logs/yes-no.txt' | tr -s ' \n' ' ' | grep -q 'is stopped. Start it when you want with'"
160  launch_model "$F29"; RC=$?
161  check "started by hand with the same choices: it loads on the new engine" "[ $RC -eq 0 ] && [ \"\$(engine_in_use | cut -d- -f1)\" = 0.1.55 ]"
162  check "and answers" "answers"
163
164  say "B2: an engine that passes every check but does not start (throwaway key): the model comes back on the old engine"
165  BADDIR="$W/bad-feed"; rm -rf "$BADDIR"; mkdir -p "$BADDIR"
166  ( cd "$REPO" && "${PY:-python3}" - "$BADDIR" "$COMP" <<'PY'
167import json, shutil, sys
168from pathlib import Path
169sys.path.insert(0, "packaging")
170import feed_tool, package_engine
171from strata_control import components, signing
172out, comp = Path(sys.argv[1]), Path(sys.argv[2])
173key = signing.generate_key()
174src = next((comp / "engine").glob("0.1.55-*"))
175work = out / "src"
176shutil.copytree(src, work, ignore=shutil.ignore_patterns("MANIFEST*", "engine-component.json", "*.sig"))
177entry = json.loads((src / "engine-component.json").read_text()).get("entrypoint", "strata")
178(work / entry).write_text("#!/bin/sh\nexit 3\n")
179(work / entry).chmod(0o755)
180folder = package_engine.build(work, "cuda", "0.1.56", components.host_platform(), out / "built", entry=entry, archs=("sm_86", "compute_86"),
181                              min_driver={"nvidia": "525.0"}, build_id="flow-bad", variant="cuda13", include=[])
182feed_tool.sign_package(folder, key)
183(out / "engine").mkdir()
184feed_tool.pack_engine(folder, out / "engine", url_prefix="engine/")
185feed_tool.build_feed(out, key=key, sequence=9999, engine_dir=out / "engine")
186(out / "trust.json").write_text(json.dumps({"keys": [signing.public_part(key)]}))
187print("bad feed written")
188PY
189  ) 2>&1 | tail -4
190  serve "$BADDIR" "$W/logs/serverbad.log"; PBAD=$PORT
191  STRATA_UPDATE_TRUSTED_KEYS="$BADDIR/trust.json" python3 "$HERE/pty_drive.py" --timeout 1800 --answer 'for the update\?.*=>y' --answer 'Start .* again on it\?.*=>y' -- \
192    "$S" update --feed "http://127.0.0.1:$PBAD/strata-feed.json" --no-control --backend cuda > "$W/logs/bad.txt" 2>&1; RC=$?
193  sed 's/\x1b\[[0-9;?]*[A-Za-z]//g' "$W/logs/bad.txt" | tr -d '\r' | grep -v '^$' | tail -16
194  check "exit 6 (failed, the old engine is in use)" "[ $RC -eq 6 ]"
195  check "it said the new engine did not start and the current one is unchanged" "sed 's/\x1b\[[0-9;?]*[A-Za-z]//g' '$W/logs/bad.txt' | tr -s ' \n' ' ' | grep -q 'did not start correctly.*current engine is unchanged'"
196  check "0.1.55 is still the engine in use" "[ \"\$(active_version)\" = 0.1.55 ]"
197  check "the model was started again on the OLD engine (0.1.55 folder)" "[ \"\$(engine_in_use | cut -d- -f1)\" = 0.1.55 ]"
198  check "and a real question is answered" "answers"
199  "$S" launch --stop 2>&1 | tail -1
200fi
201
202say "result (stage $STAGE)"
203[ "$FAIL" = 0 ] && echo "ALL PASSED" || echo "SOME FAILED"
204exit "$FAIL"