Skip to content

Qwen3-0.6B inference (12 KB), bare-metal bootable OS (22 KB), and from-scratch Mini-GPT trainer with live Braille TUI (32 KB) in 100% pure x86-64 NASM assembly. Zero C, zero libc, zero dependencies.

6.4 KB
Raw Blame
#!/bin/bash
set -euo pipefail
cd "$(dirname "$0")/.."

model="hy4-flash"
print_mode=0
detached=0
mission=""

while [[ $# -gt 0 ]]; do
    case "$1" in
        --model)
            model="${2:?missing value for --model}"
            shift 2
            ;;
        --print|-p)
            print_mode=1
            shift
            ;;
        --detached|--headless)
            detached=1
            shift
            ;;
        --mission)
            mission="${2:?missing value for --mission}"
            shift 2
            ;;
        --help|-h)
            cat <<USAGE
Usage: testbed/start_pi_arena.sh [--model <id>] [--print] [--detached] [--mission <text>]

Options:
  --model <id>     Model ID under local (default: hy4-flash)
  --print, -p      Run pi in non-interactive print mode without tmux
  --detached       Start tmux split-screen session detached and print run dir
  --mission <text> Override initial prompt from demo/pi-arena/mission.txt
USAGE
            exit 0
            ;;
        *)
            echo "Unknown argument: $1" >&2
            exit 2
            ;;
    esac
done

pi_bin="$PWD/envs/pi-arena/node_modules/.bin/pi"
ext_path="$PWD/demo/pi-arena/kvm_bridge.ts"
sys_prompt="$PWD/demo/pi-arena/system_prompt.md"
agent_dir="${PI_CODING_AGENT_DIR:-$PWD/demo/pi-arena/agent-dir}"

test -x "$pi_bin" || { echo "Missing pi binary at $pi_bin" >&2; exit 1; }
test -f "$ext_path" || { echo "Missing extension at $ext_path" >&2; exit 1; }
test -f "$agent_dir/models.json" || { echo "Missing models.json at $agent_dir/models.json" >&2; exit 1; }

if [[ -z "$mission" ]]; then
    mission=$(<"$PWD/demo/pi-arena/mission.txt")
fi

mkdir -p runs

start_qemu() {
    local run="$1"
    qemu-system-x86_64 \
        -enable-kvm -machine q35 -cpu host -m 3072 -smp 2 -no-reboot -display none \
        -serial "unix:$PWD/$run/shell.sock,server=on,wait=off" \
        -serial "unix:$PWD/$run/hud.sock,server=on,wait=off" \
        -monitor none -nic none \
        -virtfs "local,path=$PWD,mount_tag=workspace,security_model=none,id=jev" \
        -drive "file=testbed/root.ext4,format=raw,if=virtio,snapshot=on" \
        -kernel testbed/vmlinuz -initrd testbed/initrd \
        -append "console=ttyS0 panic=1 quiet root=/dev/vda rw init=/jev-init lsm=lockdown,capability,landlock,yama,apparmor,bpf jev_job=arena jev_run=${run##*/}" \
        >"$run/qemu.log" 2>&1 9>&- &
    local qpid=$!
    printf '%s\n' "$qpid" > "$run/qemu.pid"
    for ((i=0;i<100;i++)); do
        kill -0 "$qpid" 2>/dev/null || { echo "QEMU exited before sockets ready. See $run/qemu.log" >&2; return 1; }
        [[ -S "$run/shell.sock" && -S "$run/hud.sock" ]] && return 0
        sleep .05
    done
    echo "Timed out waiting for QEMU serial sockets in $run" >&2
    return 1
}

wait_ready() {
    local run="$1"
    local qpid="$2"
    for ((i=0;i<900;i++)); do
        if [[ -e "$run/ready" ]]; then
            return 0
        fi
        if [[ -n "${qpid:-}" ]] && ! kill -0 "$qpid" 2>/dev/null; then
            echo "Guest exited before startup. See $run/warden.log and $run/qemu.log" >&2
            return 1
        fi
        sleep .1
    done
    echo "Guest startup timed out. Inspect $run." >&2
    return 1
}

if [[ $print_mode -eq 1 ]]; then
    if [[ -n "${JEV_RUN_NAME:-}" ]]; then
        run="runs/$JEV_RUN_NAME"
        mkdir -p "$run"
    else
        run=$(mktemp -d "runs/$(date -u +%Y%m%dT%H%M%SZ)-pi-arena-XXXXXX")
    fi
    cleanup_print() {
        local rc=$?
        if [[ -d "$run" ]]; then
            touch "$run/stop"
        fi
        local qpid
        qpid=$(cat "$run/qemu.pid" 2>/dev/null || true)
        if [[ -n "$qpid" ]]; then
            for ((i=0;i<80;i++)); do
                kill -0 "$qpid" 2>/dev/null || break
                sleep .1
            done
            if kill -0 "$qpid" 2>/dev/null; then
                kill "$qpid" 2>/dev/null || true
            fi
        fi
        exit "$rc"
    }
    trap cleanup_print EXIT INT TERM

    start_qemu "$run"
    qpid=$(cat "$run/qemu.pid")
    wait_ready "$run" "$qpid"

    PI_CODING_AGENT_DIR="$agent_dir" \
    PI_OFFLINE=1 \
    JEV_SHELL_SOCK="$PWD/$run/shell.sock" \
    JEV_RUN_DIR="$PWD/$run" \
    "$pi_bin" \
        --print \
        --no-builtin-tools \
        --no-skills \
        --no-prompt-templates \
        --no-context-files \
        --no-session \
        --offline \
        -e "$ext_path" \
        --provider local \
        --model "$model" \
        --system-prompt "$sys_prompt" \
        "$mission" </dev/null
    exit $?
fi

socket="$PWD/runs/.pi-arena.tmux"
tm=(tmux -S "$socket")
exec 9>runs/.pi-arena-launch.lock
flock 9

if ! "${tm[@]}" has-session -t =jev 2>/dev/null; then
    if [[ -n "${JEV_RUN_NAME:-}" ]]; then
        run="runs/$JEV_RUN_NAME"
        mkdir -p "$run"
    else
        run=$(mktemp -d "runs/$(date -u +%Y%m%dT%H%M%SZ)-pi-arena-XXXXXX")
    fi
    printf '%s\n' "$run" > runs/.pi-arena-current
    start_qemu "$run"
    qpid=$(cat "$run/qemu.pid")

    left_cmd=$(cat <<LEFT
printf "Booting isolated Linux 6.12 KVM + eBPF-Jev warden (%s)...\n" $(printf '%q' "$run")
while [[ ! -e $(printf '%q' "$PWD/$run/ready") ]]; do sleep 0.1; done
PI_CODING_AGENT_DIR=$(printf '%q' "$agent_dir") \
PI_OFFLINE=1 \
JEV_SHELL_SOCK=$(printf '%q' "$PWD/$run/shell.sock") \
JEV_RUN_DIR=$(printf '%q' "$PWD/$run") \
$(printf '%q' "$pi_bin") \
    --no-builtin-tools \
    --no-skills \
    --no-prompt-templates \
    --no-context-files \
    --no-session \
    --offline \
    -e $(printf '%q' "$ext_path") \
    --provider local \
    --model $(printf '%q' "$model") \
    --system-prompt $(printf '%q' "$sys_prompt") \
    $(printf '%q' "$mission")
rc=\$?
printf "\n[pi exited rc=%d | run=%s]\n" "\$rc" $(printf '%q' "$run")
if [[ -t 0 ]]; then
    read -r -p "Press Enter to stop the arena VM and exit (or Ctrl-b d to detach)..." _ || true
fi
touch $(printf '%q' "$PWD/$run/stop")
LEFT
)
    "${tm[@]}" -f /dev/null new-session -d -s jev -c "$PWD" -x 172 -y 40 "bash -lc $(printf '%q' "$left_cmd")" 9>&-
    "${tm[@]}" set-option -t jev status off
    "${tm[@]}" split-window -dh -p 46 -t jev:0.0 -c "$PWD" "exec python3 testbed/demo-terminal.py '$run'" 9>&-
    "${tm[@]}" select-pane -t jev:0.0
else
    run=$(cat runs/.pi-arena-current)
    qpid=$(cat "$run/qemu.pid" 2>/dev/null || true)
fi
flock -u 9

if ! wait_ready "$run" "${qpid:-}"; then
    "${tm[@]}" kill-session -t =jev 2>/dev/null || true
    exit 1
fi

if [[ $detached -eq 1 ]]; then
    echo "$run"
    exit 0
fi
exec tmux -S "$socket" attach-session -t =jev