interplane.Get Omega
Read in your language

aienux

Aienux, the AienOS kernel: boot, scheduling, IPC and capability boundaries. Pre-alpha; host, emulator and physical evidence are labelled separately.

Brought by drakestapleton

branch main Branches Tags History

aienux / scripts / qemu_ck_allen_code_test.sh

283 lines · 14608 bytes Raw Blame History
1#!/usr/bin/env bash
2# qemu_ck_allen_code_test.sh -- Allen sends a coding request to the Spark and
3# shows the code that comes back, end to end, in QEMU.
4#
5# The test-computer kernel (native/kernel/x86) boots in QEMU with its second
6# serial port (COM2) joined to the relay crates/allen-courier. Keys go in
7# through the QEMU monitor. In Allen's normal chat:
8# "hello allen" Allen answers himself (talk is not code)
9# "make me a calculator" Allen says at once that he needs to code it and
10# sends it to the Spark; the code comes back in
11# pieces with a checksum and is shown (svc/allen_code.h)
12# "now add a square root key" a change to the code on the screen (code-more);
13# the reply is long and one piece is lost on the
14# way, so the kernel asks AGAIN and the whole set comes
15# Page Down, End, Home, Up the code scrolls; Escape puts it away
16# (the relay stops) "write a snake game in python": the plain line at
17# 20 s, the give-up line after 30 s of silence
18# "goodbye" the chat goes on
19# By default the relay answers code jobs from recorded replies
20# (native/kernel/svc/tests/fixtures/code, --code-reply), so the code shown is
21# compared with the recording line by line and by checksum. ALLEN_CODE_LIVE=1
22# asks the language-model server instead (127.0.0.1:18091, shared: a busy 503
23# waits exactly its Retry-After) with "make me a calculator in python", "now add
24# a square root key" and "make me a calculator in c", and reports the times:
25# keystroke to Allen's answer (ms) and request to code on the screen (s).
26#
27# Needs qemu-system-x86_64, OVMF, mtools, an x86_64 gcc and cargo (live: the
28# model server up, otherwise NOT_RUN). Takes the gate lock like every QEMU test.
29# Final line: AIENOS_ALLEN_CODE_QEMU: PASS | FAIL | NOT_RUN (reason)
30set -u
31repo_root="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)"
32cd "${repo_root}"
33tag="AIENOS_ALLEN_CODE_QEMU"
34code_fd="${OVMF_CODE:-/usr/share/OVMF/OVMF_CODE_4M.fd}"
35vars_fd="${OVMF_VARS:-/usr/share/OVMF/OVMF_VARS_4M.fd}"
36model="${ALLEN_MODEL:-127.0.0.1:18091}"
37live="${ALLEN_CODE_LIVE:-0}"
38answer_wait="${ALLEN_WAIT:-900}"
39fx="${repo_root}/native/kernel/svc/tests/fixtures/code"
40cross=""
41if [ "$(uname -m)" != "x86_64" ]; then cross="x86_64-linux-gnu-"; fi
42for t in qemu-system-x86_64 mformat mmd mcopy cargo curl "${cross}gcc" "${cross}ld"; do
43 command -v "${t}" >/dev/null || { echo "${tag}: NOT_RUN (${t} missing)"; exit 2; }
44done
45[[ -r "${code_fd}" && -r "${vars_fd}" ]] || { echo "${tag}: NOT_RUN (no OVMF)"; exit 2; }
46if [[ "${live}" == 1 ]]; then
47 if curl -s --max-time 5 "http://${model}/health" | grep -q '"status":"ok"'; then
48 echo "model server at ${model}: up"
49 else
50 echo "${tag}: NOT_RUN (no model server at ${model})"
51 exit 2
52 fi
53fi
54out="${repo_root}/target/native-kernel-allen-code"
55[[ "${live}" == 1 ]] && out="${out}-live"
56mkdir -p "${out}"
57commit="$(git rev-parse HEAD 2>/dev/null || echo unknown)"
58make -s -C native/kernel X86_CROSS="${cross}" OUT="${out}" AIENOS_COMMIT="${commit}" imac-kernel >/dev/null ||
59 { echo "${tag}: FAIL (kernel does not build)"; exit 1; }
60cargo build -q --release --offline -p allen-courier 2>"${out}/cargo.err" ||
61 { echo "${tag}: FAIL (relay does not build)"; tail -5 "${out}/cargo.err"; exit 1; }
62relay="${repo_root}/target/release/allen-courier"
63
64. "${repo_root}/scripts/lib_gate_hold.sh"
65if ! gh_quiet_check || ! gh_lock_take qemu_ck_allen_code_test "${AIENOS_GATE_MINUTES:-30}"; then
66 echo "${tag}: NOT_RUN (${gh_why})"
67 exit 3
68fi
69work="$(mktemp -d)"
70pids=()
71relay_pid=""
72cleanup() {
73 local p
74 for p in "${pids[@]}"; do kill "${p}" 2>/dev/null || true; done
75 cp "${work}/one.log" "${out}/serial.log" 2>/dev/null || true
76 cp "${work}/relay.log" "${out}/relay.log" 2>/dev/null || true
77 rm -rf "${work}"
78 gh_lock_release
79}
80trap cleanup EXIT
81
82img="${work}/esp.img"
83truncate -s 64M "${img}"
84mformat -i "${img}" -F ::
85mmd -i "${img}" ::/EFI ::/EFI/BOOT
86mcopy -i "${img}" "${out}/IMACKERN.EFI" ::/EFI/BOOT/BOOTX64.EFI
87
88# the recorded replies: 1 the calculator, 2 the long change with piece 50 lost the first time
89mkdir -p "${work}/replies"
90cp "${fx}/short.txt" "${work}/replies/1.txt"
91cp "${fx}/long.txt" "${work}/replies/2.txt"
92echo 50 >"${work}/replies/2.drop"
93
94fails=0
95pass() { echo "PASS $1"; }
96flunk() { echo "FAIL $1"; fails=$((fails + 1)); }
97chk() { # description, file, pattern
98 if grep -aqE -- "$3" "$2"; then pass "$1"; else flunk "$1 (no match for: $3)"; fi
99}
100txt() { tr -d '\r' <"${work}/one.log" 2>/dev/null || true; }
101has() { txt | grep -aqE -- "$1"; }
102count() { txt | grep -acE -- "$1"; }
103wait_for() { # seconds pattern
104 local i
105 for ((i = 0; i < $1 * 5; i++)); do
106 has "$2" && return 0
107 sleep 0.2
108 done
109 return 1
110}
111wait_count() { # seconds pattern n: until the pattern has matched n times
112 local i
113 for ((i = 0; i < $1 * 5; i++)); do
114 [[ "$(count "$2")" -ge "$3" ]] && return 0
115 sleep 0.2
116 done
117 return 1
118}
119key() { echo "sendkey $1" >&3; sleep 0.3; }
120say() { # text: typed through the monitor, then Enter (letters, digits, spaces)
121 local text="$1" c k i
122 for ((i = 0; i < ${#text}; i++)); do
123 c="${text:i:1}"
124 case "${c}" in
125 " ") k=spc ;;
126 [a-z0-9]) k="${c}" ;;
127 *) echo "say: no key for '${c}'" >&2; exit 2 ;;
128 esac
129 echo "sendkey ${k}" >&3
130 sleep 0.15
131 done
132 echo "sendkey ret" >&3
133}
134shot() { # name
135 rm -f "${work}/$1.png"
136 echo "screendump ${work}/$1.png -f png" >&3
137 for _ in $(seq 1 50); do [[ -s "${work}/$1.png" ]] && break; sleep 0.2; done
138 sleep 0.5
139 [[ -s "${work}/$1.png" ]] && cp "${work}/$1.png" "${out}/$1.png"
140}
141# the code| lines the kernel echoed after its n-th "code shown"
142shown() { txt | awk -v n="$1" '/^allen-code: code shown/{b++} b==n && /^code\|/{sub(/^code\|/,""); print}'; }
143# the first 40 lines of a recording's code block
144recorded() { awk 'f && /^```/{exit} f{print} /^```/{f=1}' "$1" | head -40; }
145
146cp "${vars_fd}" "${work}/one.vars"
147mkfifo "${work}/one.mon.in" "${work}/one.mon.out"
148: >"${work}/one.log"
149qemu-system-x86_64 -M q35 -accel tcg -cpu max -smp 2 -m 1024 \
150 -drive if=pflash,format=raw,readonly=on,file="${code_fd}" \
151 -drive if=pflash,format=raw,file="${work}/one.vars" \
152 -drive if=none,id=esp,format=raw,file="${img}" -device ide-hd,drive=esp,bus=ide.0 \
153 -device qemu-xhci,id=xhci -device usb-kbd,bus=xhci.0,port=1 \
154 -chardev pipe,id=mon,path="${work}/one.mon" -mon chardev=mon,mode=readline \
155 -display none -nic none \
156 -serial file:"${work}/one.log" \
157 -serial unix:"${work}/wire.sock",server=on,wait=off \
158 -no-reboot 2>"${work}/one.err" &
159pids+=($!)
160cat "${work}/one.mon.out" >/dev/null &
161pids+=($!)
162if [[ "${live}" == 1 ]]; then
163 "${relay}" --socket "${work}/wire.sock" --model "${model}" --work "${work}/answers" --log-dir "${work}/log" \
164 --busy-wait "${answer_wait}" >"${work}/relay.log" 2>&1 &
165else
166 "${relay}" --socket "${work}/wire.sock" --model 127.0.0.1:9 --work "${work}/answers" --log-dir "${work}/log" \
167 --code-reply "${work}/replies" >"${work}/relay.log" 2>&1 &
168fi
169relay_pid=$!
170pids+=("${relay_pid}")
171
172typed=0
173if wait_for 300 '^keyboard: type a line and press Enter'; then
174 exec 3>"${work}/one.mon.in"
175 wait_for 30 '^courier: ready, the far side said hello' || true
176 wait_for 60 '^screen: night: .* by 20 s after Allen woke' || true
177 say "hello allen"
178 typed=1
179 wait_for 60 '^allen: Hello Humanity\.$' || true
180 if [[ "${live}" == 1 ]]; then
181 asks=("make me a calculator in python" "now add a square root key" "make me a calculator in c")
182 for i in 0 1 2; do
183 say "${asks[$i]}"
184 wait_count "${answer_wait}" '^allen-code: (code shown|the Spark stopped|the Spark answered without|lost)' $((i + 1)) || true
185 sleep 2
186 shot "code-live-$((i + 1))"
187 key pgdn
188 sleep 1
189 shot "code-live-$((i + 1))-pgdn"
190 done
191 key esc
192 else
193 say "make me a calculator"
194 wait_count 120 '^allen-code: (code shown|the Spark stopped|lost)' 1 || true
195 sleep 2
196 shot code-short
197 say "now add a square root key"
198 wait_count 120 '^allen-code: (code shown|the Spark stopped|lost)' 2 || true
199 sleep 2
200 shot code-long
201 key pgdn
202 key pgdn
203 sleep 1.5
204 shot code-long-pgdn
205 key end
206 key home
207 key down
208 key up
209 key esc
210 wait_for 10 '^allen-code: code put away' || true
211 sleep 1
212 shot code-away
213 # the Spark goes quiet: the relay stops
214 kill "${relay_pid}" 2>/dev/null || true
215 sleep 1
216 say "write a snake game in python"
217 wait_for 25 '^allen-code: the Spark is slow' || true
218 shot code-waiting
219 wait_for 20 '^allen-code: the Spark stopped answering' || true
220 say "goodbye"
221 wait_for 30 '^power: off via ACPI S5 ' || true
222 fi
223 exec 3>&-
224fi
225txt >"${work}/one.txt"
226T="${work}/one.txt"
227
228[[ ${typed} == 1 ]] && pass "keys typed from outside the guest" || flunk "the keyboard never came up"
229chk "the far side said hello" "$T" '^courier: ready, the far side said hello$'
230chk "talk is still talk: Allen answers the greeting himself" "$T" '^allen: Hello Humanity\.$'
231if [[ "${live}" == 1 ]]; then
232 chk "Allen says at once he needs to code it" "$T" '^allen: I need to code that\. Let me send it off to the Spark\.$'
233 chk "the change goes as a change" "$T" '^allen: Let me take that change to the Spark\.$'
234 [[ "$(count '^allen-code: code shown, [1-9][0-9]* lines in [0-9]+ ms$')" == 3 ]] && pass "three answers shown as code" ||
235 flunk "not three answers shown as code ($(count '^allen-code: code shown'))"
236 chk "a python calculator" "$T" '^allen-code: [0-9]+ bytes, crc32 [0-9a-f]{8}, [0-9]+ pieces, language python$'
237 chk "a C calculator" "$T" '^allen-code: [0-9]+ bytes, crc32 [0-9a-f]{8}, [0-9]+ pieces, language c$'
238 if shown 2 | grep -aqiE 'sqrt|square'; then pass "the change added a square root"; else flunk "no square root in the changed code"; fi
239 chk "the relay asked the model for code" "${work}/relay.log" '^courier: serial job [0-9]+ \(code, '
240 chk "the relay asked for a change with the code before" "${work}/relay.log" '^courier: serial job [0-9]+ \(code-more, '
241else
242 chk "Allen says at once he needs to code it" "$T" '^allen: I need to code that\. Let me send it off to the Spark\.$'
243 chk "the request reported by length only" "$T" '^allen-code: asked the Spark \(20 chars\)$'
244 chk "the acknowledgement timed" "$T" '^allen-code: acknowledged in [0-9]+ ms \(job 1, new code\)$'
245 chk "the calculator shown" "$T" '^allen-code: code shown, 29 lines in [0-9]+ ms$'
246 crc1=$("${relay}" --split "${fx}/short.txt" | head -1 | awk '{print $5}')
247 crc2=$("${relay}" --split "${fx}/long.txt" | head -1 | awk '{print $5}')
248 chk "its checksum is the recording's" "$T" "^allen-code: 768 bytes, crc32 ${crc1}, 6 pieces, language python$"
249 if diff <(shown 1) <(recorded "${fx}/short.txt") >"${work}/d1"; then pass "the code read back is the recording, line by line"; else flunk "the code read back differs: $(head -3 "${work}/d1" | tr '\n' ' ')"; fi
250 chk "Allen's own sentence after the code" "$T" '^allen: Here is a small calculator you can run with python3 calc\.py\.$'
251 chk "the change goes as a change" "$T" '^allen: Let me take that change to the Spark\.$'
252 chk "the relay left piece 50 off" "${work}/relay.log" '^courier: code job 2: piece 50 left off'
253 chk "the kernel saw the piece missing and asked again" "$T" '^allen-code: a piece went missing, asked again$'
254 chk "the relay sent the set again" "${work}/relay.log" '^courier: serial: code job 2 sent again \(143 lines\)$'
255 chk "the long code shown whole" "$T" '^allen-code: code shown, 730 lines in [0-9]+ ms$'
256 chk "its checksum is the recording's" "$T" "^allen-code: 18056 bytes, crc32 ${crc2}, 142 pieces, language c$"
257 if diff <(shown 2) <(recorded "${fx}/long.txt") >"${work}/d2"; then pass "the long code read back is the recording"; else flunk "the long code read back differs: $(head -3 "${work}/d2" | tr '\n' ' ')"; fi
258 pg=$(txt | grep -aE '^allen-code: rows ' | head -1)
259 [[ "${pg}" =~ ^allen-code:\ rows\ ([0-9]+)\ to\ ([0-9]+)\ of\ ([0-9]+)\ shown$ ]] && (( BASH_REMATCH[1] > 1 )) &&
260 pass "Page Down moves the code (${pg#allen-code: })" || flunk "Page Down did not move the code (${pg})"
261 last=$(txt | grep -aE '^allen-code: rows ' | sed -n 3p)
262 [[ "${last}" =~ to\ ([0-9]+)\ of\ ([0-9]+)\ shown$ ]] && [[ "${BASH_REMATCH[1]}" == "${BASH_REMATCH[2]}" ]] &&
263 pass "End shows the last row (${last#allen-code: })" || flunk "End did not reach the end (${last})"
264 chk "Home goes back to the top" "$T" '^allen-code: rows 1 to [0-9]+ of [0-9]+ shown$'
265 chk "Escape puts the code away" "$T" '^allen-code: code put away$'
266 chk "with the Spark quiet: the plain line at 20 s" "$T" '^allen: The Spark is still writing it\. Longer code takes a little while\.$'
267 chk "and the give-up line after 30 s of silence" "$T" '^allen: The Spark stopped answering, so I couldn.t get the code\. Try again in a minute\.$'
268 chk "the chat goes on after it" "$T" '^allen: Goodbye\. Until next time\.$'
269 for f in code-short code-long code-long-pgdn code-away code-waiting; do
270 [[ -s "${out}/${f}.png" ]] && pass "screenshot ${f}.png" || flunk "no screenshot ${f}.png"
271 done
272fi
273# the request's words and the code never reach a line the lab report may carry
274if grep -aE '^(screen|allen-code|courier): ' "$T" | grep -aqiE 'calculator|square|snake|import |#include'; then
275 flunk "words of the request or code leaked into a report line"
276else
277 pass "report lines carry lengths and times only"
278fi
279if LC_ALL=C grep -aqE '^allen: .*[^ -~]' "$T"; then flunk "a character the typeface cannot draw reached Allen's words"; else pass "Allen's words are plain letters"; fi
280echo "the kernel's times: $(grep -aE '^allen-code: (acknowledged|code shown)' "$T" | sed 's/^allen-code: //' | tr '\n' ';')"
281echo "the relay: $(grep -aE '^courier: (serial job|code job)' "${work}/relay.log" | tr '\n' ';')"
282echo "serial log: ${out}/serial.log; relay log: ${out}/relay.log; screenshots: ${out}/*.png"
283if [[ ${fails} == 0 ]]; then echo "${tag}: PASS"; else echo "${tag}: FAIL (${fails})"; exit 1; fi