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_courier_test.sh

182 lines · 10764 bytes Raw Blame History
1#!/usr/bin/env bash
2# qemu_ck_allen_courier_test.sh -- Allen asks a far-off worker and brings the
3# answer back, end to end, in QEMU.
4#
5# The test-computer kernel (native/kernel/x86) boots in QEMU with a second
6# serial port (COM2) joined to a Unix socket. The relay crates/allen-courier
7# sits on that socket and carries Allen's job lines to the language-model
8# server on this machine (127.0.0.1:18091 by default, the OpenAI chat shape)
9# and the answers back. Keys go in through the QEMU monitor: "hello allen"
10# (the very first meeting: Allen answers that himself, the naming exchange),
11# then "write a haiku about rain" and "what did i just ask you". The model
12# answers BOTH of those (only goodbye and the naming are Allen's own), so
13# Allen writes a job for each, keeps thinking, and says what came back. After
14# the first exchange he keeps a note of it, and the second job carries that
15# note to the model; the relay keeps every exchange in its learning log. The
16# model server is shared and answers "busy" (503) while other jobs run, so the
17# relay keeps asking (ALLEN_BUSY_WAIT seconds, default 900) and the test waits
18# as long (ALLEN_WAIT seconds per answer, default 900). The real Ethernet wire cannot
19# run in QEMU (no Broadcom chip): scripts/ck_courier_udp_test.sh covers it.
20#
21# Needs qemu-system-x86_64, OVMF, mtools, an x86_64 gcc, cargo, and the model
22# server up (otherwise NOT_RUN). Takes the gate lock like every QEMU test.
23# Final line: AIENOS_ALLEN_COURIER: PASS | FAIL | NOT_RUN (reason)
24set -u
25repo_root="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)"
26cd "${repo_root}"
27tag="AIENOS_ALLEN_COURIER"
28code_fd="${OVMF_CODE:-/usr/share/OVMF/OVMF_CODE_4M.fd}"
29vars_fd="${OVMF_VARS:-/usr/share/OVMF/OVMF_VARS_4M.fd}"
30model="${ALLEN_MODEL:-127.0.0.1:18091}"
31busy_wait="${ALLEN_BUSY_WAIT:-900}"
32answer_wait="${ALLEN_WAIT:-900}"
33cross=""
34if [ "$(uname -m)" != "x86_64" ]; then cross="x86_64-linux-gnu-"; fi
35for t in qemu-system-x86_64 mformat mmd mcopy cargo curl "${cross}gcc" "${cross}ld"; do
36 command -v "${t}" >/dev/null || { echo "${tag}: NOT_RUN (${t} missing)"; exit 2; }
37done
38[[ -r "${code_fd}" && -r "${vars_fd}" ]] || { echo "${tag}: NOT_RUN (no OVMF)"; exit 2; }
39if ! curl -s --max-time 5 "http://${model}/health" | grep -q '"status":"ok"'; then
40 echo "${tag}: NOT_RUN (no model server at ${model})"
41 exit 2
42fi
43out="${repo_root}/target/native-kernel-allen-courier"
44mkdir -p "${out}"
45commit="$(git rev-parse HEAD 2>/dev/null || echo unknown)"
46make -s -C native/kernel X86_CROSS="${cross}" OUT="${out}" AIENOS_COMMIT="${commit}" imac-kernel >/dev/null ||
47 { echo "${tag}: FAIL (kernel does not build)"; exit 1; }
48cargo build -q --release --offline -p allen-courier 2>"${out}/cargo.err" ||
49 { echo "${tag}: FAIL (relay does not build)"; tail -5 "${out}/cargo.err"; exit 1; }
50relay="${repo_root}/target/release/allen-courier"
51
52. "${repo_root}/scripts/lib_gate_hold.sh"
53if ! gh_quiet_check || ! gh_lock_take qemu_ck_allen_courier_test "${AIENOS_GATE_MINUTES:-20}"; then
54 echo "${tag}: NOT_RUN (${gh_why})"
55 exit 3
56fi
57work="$(mktemp -d)"
58pids=()
59cleanup() {
60 local p
61 for p in "${pids[@]}"; do kill "${p}" 2>/dev/null || true; done
62 cp "${work}/one.log" "${out}/serial.log" 2>/dev/null || true
63 cp "${work}/relay.log" "${out}/relay.log" 2>/dev/null || true
64 rm -rf "${work}"
65 gh_lock_release
66}
67trap cleanup EXIT
68
69img="${work}/esp.img"
70truncate -s 64M "${img}"
71mformat -i "${img}" -F ::
72mmd -i "${img}" ::/EFI ::/EFI/BOOT
73mcopy -i "${img}" "${out}/IMACKERN.EFI" ::/EFI/BOOT/BOOTX64.EFI
74
75fails=0
76chk() { # description, file, pattern
77 if grep -aqE -- "$3" "$2"; then echo "PASS $1"; else echo "FAIL $1 (no match for: $3)"; fails=$((fails + 1)); fi
78}
79txt() { tr -d '\r' <"${work}/one.log" 2>/dev/null || true; }
80has() { txt | grep -aqE -- "$1"; }
81wait_for() { # seconds pattern
82 local i
83 for ((i = 0; i < $1 * 5; i++)); do
84 has "$2" && return 0
85 sleep 0.2
86 done
87 return 1
88}
89say() { # text: typed through the monitor, then Enter (letters, digits, spaces)
90 local text="$1" c k i
91 for ((i = 0; i < ${#text}; i++)); do
92 c="${text:i:1}"
93 case "${c}" in
94 " ") k=spc ;;
95 [a-z0-9]) k="${c}" ;;
96 *) echo "say: no key for '${c}'" >&2; exit 2 ;;
97 esac
98 echo "sendkey ${k}" >&3
99 sleep 0.15
100 done
101 echo "sendkey ret" >&3
102}
103
104cp "${vars_fd}" "${work}/one.vars"
105mkfifo "${work}/one.mon.in" "${work}/one.mon.out"
106: >"${work}/one.log"
107qemu-system-x86_64 -M q35 -accel tcg -cpu max -smp 2 -m 1024 \
108 -drive if=pflash,format=raw,readonly=on,file="${code_fd}" \
109 -drive if=pflash,format=raw,file="${work}/one.vars" \
110 -drive if=none,id=esp,format=raw,file="${img}" -device ide-hd,drive=esp,bus=ide.0 \
111 -device qemu-xhci,id=xhci -device usb-kbd,bus=xhci.0,port=1 \
112 -chardev pipe,id=mon,path="${work}/one.mon" -mon chardev=mon,mode=readline \
113 -display none -nic none \
114 -serial file:"${work}/one.log" \
115 -serial unix:"${work}/wire.sock",server=on,wait=off \
116 -no-reboot 2>"${work}/one.err" &
117pids+=($!)
118cat "${work}/one.mon.out" >/dev/null &
119pids+=($!)
120"${relay}" --socket "${work}/wire.sock" --model "${model}" --work "${work}/answers" --log-dir "${work}/log" --busy-wait "${busy_wait}" >"${work}/relay.log" 2>&1 &
121pids+=($!)
122
123typed=0
124if wait_for 300 '^keyboard: type a line and press Enter'; then
125 exec 3>"${work}/one.mon.in"
126 wait_for 30 '^courier: ready, the far side said hello' || true
127 wait_for 60 '^screen: night: .* by 20 s after Allen woke' || true # he has formed; the picture will show him
128 say "hello allen"
129 typed=1
130 wait_for 60 '^allen: Hello Humanity\.$' || true
131 say "write a haiku about rain"
132 wait_for "${answer_wait}" '^screen: the far side (answered job 1|did not answer job 1)' || true
133 sleep 1
134 say "what did i just ask you"
135 wait_for "${answer_wait}" '^screen: the far side (answered job 2|did not answer job 2)' || true
136 sleep 2.5 # his words are still coming when the picture is taken
137 echo "screendump ${work}/one.png -f png" >&3
138 for _ in $(seq 1 50); do [[ -s "${work}/one.png" ]] && break; sleep 0.2; done
139 sleep 1
140 [[ -s "${work}/one.png" ]] && cp "${work}/one.png" "${out}/allen-far-side.png"
141 say "goodbye"
142 wait_for 30 '^power: off via ACPI S5 ' || true
143 exec 3>&-
144fi
145txt >"${work}/one.txt"
146
147[[ ${typed} == 1 ]] && echo "PASS keys typed from outside the guest" || { echo "FAIL the keyboard never came up"; fails=$((fails + 1)); }
148chk "a second serial port found at boot" "${work}/one.txt" '^courier: a second serial port answers'
149chk "the far side said hello" "${work}/one.txt" '^courier: ready, the far side said hello$'
150chk "Allen answers the very first greeting himself" "${work}/one.txt" '^allen: Hello Humanity\.$'
151chk "the first question goes to the far side as job 1" "${work}/one.txt" '^allen: \(asking the far side, job 1\)$'
152chk "the second question goes as job 2" "${work}/one.txt" '^allen: \(asking the far side, job 2\)$'
153chk "job 1 reported by lengths only" "${work}/one.txt" '^screen: Allen asked the far side \(24 characters in, [0-9]+ in the job\)$'
154chk "job 2 reported by lengths only" "${work}/one.txt" '^screen: Allen asked the far side \(23 characters in, [0-9]+ in the job\)$'
155chk "the far side began job 1" "${work}/one.txt" '^screen: the far side began job 1 after [0-9]+ ms$'
156chk "the far side answered job 1" "${work}/one.txt" '^screen: the far side answered job 1 \([1-9][0-9]* characters out, [1-9][0-9]* parts, [0-9]+ ms\)$'
157chk "the far side answered job 2" "${work}/one.txt" '^screen: the far side answered job 2 \([1-9][0-9]* characters out, [1-9][0-9]* parts, [0-9]+ ms\)$'
158chk "Allen said an answer (not the empty one)" "${work}/one.txt" '^allen: [^(].*'
159chk "Allen kept a note of the first exchange" "${work}/one.txt" '^allen_memory: note [0-9]+ \(the exchange\) (saved|kept until power off) in [0-9]+ ms$'
160if grep -aqE '^allen: (The far side had nothing to say\.|I lost my thread)' "${work}/one.txt"; then echo "FAIL the far side had nothing to say"; fails=$((fails + 1)); fi
161chk "the relay carried job 1 and said it back" "${work}/relay.log" '^courier: serial job 1 \(make, 24 chars asked, [0-9]+ in the instruction, [0-9]+ memory lines\) -> [1-9][0-9]* chars, [1-9][0-9]* parts, first in [0-9]+ ms, whole in [0-9]+ ms, waits [0-9]+, status 200$'
162chk "the relay carried job 2 and said it back" "${work}/relay.log" '^courier: serial job 2 \(chat, 2[23] chars asked, [0-9]+ in the instruction, [0-9]+ memory lines\) -> [1-9][0-9]* chars, [1-9][0-9]* parts, first in [0-9]+ ms, whole in [0-9]+ ms, waits [0-9]+, status 200$'
163log="$(ls "${work}"/log/*.jsonl 2>/dev/null | head -1)"
164[[ -n "${log}" && "$(wc -l <"${log}")" == 2 ]] && echo "PASS the learning log holds both exchanges" || { echo "FAIL the learning log does not hold two exchanges"; fails=$((fails + 1)); }
165[[ -n "${log}" ]] && chk "the learning log keeps the questions" "${log}" '"ask":"write a haiku about rain"'
166[[ -n "${log}" ]] && chk "the second job carried the first exchange as a note" <(sed -n 2p "${log}") '"memory":\[[^]]*note asked: write a haiku about rain'
167# Check the actual second answer, separately from the question and memory
168# fields. Carrying the note alone does not prove that Allen recalled it.
169recall=""
170[[ -n "${log}" ]] && recall=$(sed -n 2p "${log}" | grep -aoE '"reply":"([^"\\]|\\.)*"' || true)
171chk "the second answer recalls the requested form" <(printf '%s\n' "${recall}") '[Hh][Aa][Ii][Kk][Uu]'
172chk "the second answer recalls the requested topic" <(printf '%s\n' "${recall}") '[Rr][Aa][Ii][Nn]'
173[[ -n "${log}" ]] && { mkdir -p "${out}/log"; cp "${log}" "${out}/log/"; }
174chk "Allen says goodbye" "${work}/one.txt" '^allen: Goodbye\. Until next time\.$'
175[[ -s "${out}/allen-far-side.png" ]] && echo "PASS screenshot kept (${out}/allen-far-side.png)" || { echo "FAIL no screenshot"; fails=$((fails + 1)); }
176# the words of the answer never appear in a "screen:" line (the lab report's channel)
177if grep -aE '^screen: ' "${work}/one.txt" | grep -aqiE 'rain|haiku'; then echo "FAIL answer words leaked into a screen: line"; fails=$((fails + 1)); else echo "PASS screen: lines carry lengths only"; fi
178if LC_ALL=C grep -aqE '^allen: .*[^ -~]' "${work}/one.txt"; then echo "FAIL a character the typeface cannot draw reached Allen's words"; fails=$((fails + 1)); else echo "PASS Allen's words are plain letters"; fi
179echo "relay said: $(grep -aE '^courier: serial job' "${work}/relay.log")"
180echo "the kernel's own times: $(grep -aE '^screen: the far side (began|answered)' "${work}/one.txt" | tr '\n' ';')"
181echo "serial log: ${out}/serial.log; relay log: ${out}/relay.log"
182if [[ ${fails} == 0 ]]; then echo "${tag}: PASS"; else echo "${tag}: FAIL (${fails})"; exit 1; fi