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.

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

model="${SWARM_MODEL_ALIAS:-inkling-2-mini-iq3s}"
headless=0
detached=0
timeout_sec=240
base_port="${SWARM_BASE_PORT:-18100}"

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

Modes:
  (default)            5-pane interactive tmux layout (left 60% = 2x2 live pi.dev TUIs for GPU 0..3,
                       right 40% full-height = live demo/tui_hud.py)
  --detached           Create the 5-pane tmux layout detached and print run dir
  --headless           Run all 4 pi.dev agents concurrently (--print), save logs & swarm_report.md,
                       then stop the temporary KVM cleanly
  --timeout <sec>      Outer timeout for --headless mode (default: 240)
  --model <id>         Served model alias on :18100..:18103 (default: inkling-2-mini-iq3s)
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-swarm/kvm_swarm_bridge.ts"
mux_script="$PWD/demo/pi-swarm/kvm_mux.py"
report_script="$PWD/demo/pi-swarm/generate_report.py"
agent_dir="$PWD/demo/pi-swarm/agent-dir"
roles_dir="$PWD/demo/pi-swarm/roles"

for req in "$pi_bin" "$ext_path" "$mux_script" "$report_script" "$agent_dir/models.json"; do
    [[ -e "$req" ]] || { echo "Missing required file: $req" >&2; exit 1; }
done

# Verify all 4 single-GPU model servers are up on 127.0.0.1:18100..18103
for gpu in 0 1 2 3; do
    port=$((base_port + gpu))
    if ! curl -sf --max-time 3 "http://127.0.0.1:${port}/v1/models" | grep -q "\"${model}\""; then
        echo "Model '${model}' is not ready on http://127.0.0.1:${port}/v1/models." >&2
        echo "Start the 4 single-GPU servers first with: testbed/start_swarm_models.sh" >&2
        exit 1
    fi
done

ROLE_NAMES=(
    "Environment & Surface Inspector"
    "Socket & Protocol Matrix Tester"
    "Compiled C Syscall Tester"
    "IPC & Coordinator"
)

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 0.05
    done
    echo "Timed out waiting for QEMU serial sockets in $run" >&2
    return 1
}

start_mux() {
    local run="$1"
    python3 "$mux_script" "$PWD/$run" >"$run/mux.log" 2>&1 9>&- &
    local mpid=$!
    printf '%s\n' "$mpid" > "$run/mux.pid"
}

wait_mux_ready() {
    local run="$1"
    local qpid="$2"
    local mpid="$3"
    for ((i=0; i<1000; i++)); do
        [[ -e "$run/mux.ready" ]] && return 0
        if [[ -n "$qpid" ]] && ! kill -0 "$qpid" 2>/dev/null; then
            echo "QEMU exited before mux ready. See $run/qemu.log and $run/warden.log" >&2
            return 1
        fi
        if [[ -n "$mpid" ]] && ! kill -0 "$mpid" 2>/dev/null; then
            echo "kvm_mux.py exited before ready. See $run/mux.log" >&2
            return 1
        fi
        sleep 0.1
    done
    echo "Timed out waiting for KVM + mux ready in $run" >&2
    return 1
}

stop_run_vm() {
    local run="$1"
    local mpid qpid
    mpid=$(cat "$run/mux.pid" 2>/dev/null || true)
    if [[ -n "$mpid" ]] && kill -0 "$mpid" 2>/dev/null; then
        kill -TERM "$mpid" 2>/dev/null || true
        for ((i=0; i<40; i++)); do
            kill -0 "$mpid" 2>/dev/null || break
            sleep 0.1
        done
        kill -KILL "$mpid" 2>/dev/null || true
    fi
    if [[ -d "$run" ]]; then
        touch "$run/stop"
    fi
    qpid=$(cat "$run/qemu.pid" 2>/dev/null || true)
    if [[ -n "$qpid" ]] && kill -0 "$qpid" 2>/dev/null; then
        for ((i=0; i<120; i++)); do
            kill -0 "$qpid" 2>/dev/null || break
            sleep 0.1
        done
        if kill -0 "$qpid" 2>/dev/null; then
            kill -TERM "$qpid" 2>/dev/null || true
            sleep 0.5
            kill -KILL "$qpid" 2>/dev/null || true
        fi
    fi
}

if [[ $headless -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-swarm-XXXXXX")
    fi
    printf '%s\n' "$run" > runs/.pi-swarm-current

    agent_pids=()
    cleanup_headless() {
        local rc=$?
        for pid in "${agent_pids[@]:-}"; do
            if [[ -n "$pid" ]] && kill -0 "$pid" 2>/dev/null; then
                kill -TERM "$pid" 2>/dev/null || true
            fi
        done
        stop_run_vm "$run"
        exit "$rc"
    }
    trap cleanup_headless INT TERM

    started_utc=$(date -u +%Y-%m-%dT%H:%M:%SZ)
    t_start=$SECONDS
    echo "[pi-swarm] Booting hardened Linux 6.12 KVM in $run ..."
    start_qemu "$run"
    qpid=$(cat "$run/qemu.pid")
    start_mux "$run"
    mpid=$(cat "$run/mux.pid")
    if ! wait_mux_ready "$run" "$qpid" "$mpid"; then
        stop_run_vm "$run"
        exit 1
    fi
    echo "[pi-swarm] KVM + mux.sock READY. Launching 4 parallel pi.dev QA agents (timeout=${timeout_sec}s)..."

    for i in 0 1 2 3; do
        role="${ROLE_NAMES[$i]}"
        sys_prompt="$roles_dir/agent-${i}.system.md"
        mission=$(<"$roles_dir/agent-${i}.mission.txt")
        (
            exec timeout --signal=TERM --kill-after=10 "${timeout_sec}s" \
                env \
                PI_CODING_AGENT_DIR="$agent_dir" \
                PI_OFFLINE=1 \
                JEV_MUX_SOCK="$PWD/$run/mux.sock" \
                JEV_RUN_DIR="$PWD/$run" \
                JEV_AGENT_ID="agent-${i}" \
                JEV_AGENT_ROLE="$role" \
                JEV_AGENT_GPU="$i" \
                "$pi_bin" \
                    --print \
                    --no-builtin-tools \
                    --no-skills \
                    --no-prompt-templates \
                    --no-context-files \
                    --no-session \
                    --offline \
                    -e "$ext_path" \
                    --provider "gpu${i}" \
                    --model "$model" \
                    --system-prompt "$sys_prompt" \
                    "$mission" </dev/null >"$run/agent-${i}.log" 2>&1
        ) &
        agent_pids[$i]=$!
        echo "[pi-swarm] started agent-${i} (gpu${i} :$((base_port + i)), pid=${agent_pids[$i]}, role=${role})"
    done

    # Monitor until all 4 finish or secret_leaked triggers early stop
    early_leak=0
    while true; do
        alive=0
        for i in 0 1 2 3; do
            if kill -0 "${agent_pids[$i]}" 2>/dev/null; then
                alive=$((alive + 1))
            fi
        done
        if [[ $alive -eq 0 ]]; then
            break
        fi
        if [[ -f "$run/counters.json" ]] && grep -q '"secret_leaked": true' "$run/counters.json" 2>/dev/null; then
            early_leak=1
            echo "[pi-swarm] ALERT: secret_leaked=true detected! Allowing 3s grace then stopping agents..."
            sleep 3
            for i in 0 1 2 3; do
                kill -TERM "${agent_pids[$i]}" 2>/dev/null || true
            done
            break
        fi
        sleep 1
    done

    declare -a exit_codes=(0 0 0 0)
    for i in 0 1 2 3; do
        set +e
        wait "${agent_pids[$i]}"
        exit_codes[$i]=$?
        set -e
        echo "[pi-swarm] agent-${i} finished (rc=${exit_codes[$i]})"
    done

    finished_utc=$(date -u +%Y-%m-%dT%H:%M:%SZ)
    duration_s=$((SECONDS - t_start))

    # Capture HUD snapshot while files are finalized
    python3 demo/tui_hud.py "$run" --snapshot > "$run/hud.txt" 2>&1 || true

    # Cleanly stop mux and temporary KVM so warden flushes dropped=0 to warden.log
    stop_run_vm "$run"
    trap - INT TERM

    cat > "$run/swarm_meta.json" <<METAJSON
{
  "run_dir": "$PWD/$run",
  "started_utc": "$started_utc",
  "finished_utc": "$finished_utc",
  "duration_s": $duration_s,
  "timeout_s": $timeout_sec,
  "model": "$model",
  "early_leak": $early_leak,
  "agent_exit_codes": {
    "agent-0": ${exit_codes[0]},
    "agent-1": ${exit_codes[1]},
    "agent-2": ${exit_codes[2]},
    "agent-3": ${exit_codes[3]}
  }
}
METAJSON

    python3 "$report_script" "$run" --model "$model" --timeout "$timeout_sec"
    echo "[pi-swarm] Run complete: $PWD/$run"
    echo "[pi-swarm] Report:       $PWD/$run/swarm_report.md"
    exit 0
fi

# --- Interactive / Detached 5-Pane tmux Mode ---
socket="$PWD/runs/.pi-swarm.tmux"
tm=(tmux -S "$socket")
exec 9>runs/.pi-swarm-launch.lock
flock 9

if ! "${tm[@]}" has-session -t =jev-swarm 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-swarm-XXXXXX")
    fi
    printf '%s\n' "$run" > runs/.pi-swarm-current
    start_qemu "$run"
    qpid=$(cat "$run/qemu.pid")
    start_mux "$run"
    mpid=$(cat "$run/mux.pid")

    make_pane_cmd() {
        local idx="$1"
        local role="${ROLE_NAMES[$idx]}"
        local sys_prompt="$roles_dir/agent-${idx}.system.md"
        local mission
        mission=$(<"$roles_dir/agent-${idx}.mission.txt")
        cat <<PANE
printf "Booting isolated Linux 6.12 KVM + mux.sock (%s | GPU %d)...\n" $(printf '%q' "$run") "$idx"
while [[ ! -e $(printf '%q' "$PWD/$run/mux.ready") ]]; do sleep 0.1; done
PI_CODING_AGENT_DIR=$(printf '%q' "$agent_dir") \
PI_OFFLINE=1 \
JEV_MUX_SOCK=$(printf '%q' "$PWD/$run/mux.sock") \
JEV_RUN_DIR=$(printf '%q' "$PWD/$run") \
JEV_AGENT_ID=$(printf '%q' "agent-${idx}") \
JEV_AGENT_ROLE=$(printf '%q' "$role") \
JEV_AGENT_GPU=$(printf '%q' "$idx") \
$(printf '%q' "$pi_bin") \
    --no-builtin-tools \
    --no-skills \
    --no-prompt-templates \
    --no-context-files \
    --no-session \
    --offline \
    -e $(printf '%q' "$ext_path") \
    --provider $(printf '%q' "gpu${idx}") \
    --model $(printf '%q' "$model") \
    --system-prompt $(printf '%q' "$sys_prompt") \
    $(printf '%q' "$mission")
rc=\$?
printf "\n[agent-%d exited rc=%d | Ctrl-b d to detach | testbed/stop_pi_swarm.sh to stop]\n" "$idx" "\$rc"
exec bash -i
PANE
    }

    cmd0=$(make_pane_cmd 0)
    cmd1=$(make_pane_cmd 1)
    cmd2=$(make_pane_cmd 2)
    cmd3=$(make_pane_cmd 3)
    hud_cmd=$(cat <<HUD
printf "Waiting for eBPF-Jev warden in %s...\n" $(printf '%q' "$run")
while [[ ! -e $(printf '%q' "$PWD/$run/ready") ]]; do sleep 0.1; done
exec python3 demo/tui_hud.py $(printf '%q' "$PWD/$run")
HUD
)

    p_a0=$("${tm[@]}" -f /dev/null new-session -d -P -F '#{pane_id}' -s jev-swarm -c "$PWD" -x 220 -y 56 "bash -lc $(printf '%q' "$cmd0")" 9>&-)
    "${tm[@]}" set-option -t jev-swarm status off
    "${tm[@]}" set-option -t jev-swarm mouse on
    "${tm[@]}" set-window-option -t jev-swarm pane-border-status top
    "${tm[@]}" set-window-option -t jev-swarm pane-border-format " #{@pane_label} "

    # Right 40% full-height: live demo/tui_hud.py
    p_hud=$("${tm[@]}" split-window -dh -P -F '#{pane_id}' -l 40% -t "$p_a0" -c "$PWD" "bash -lc $(printf '%q' "$hud_cmd")" 9>&-)

    # Split left 60% into 2x2 grid for GPU 0..3
    p_a1=$("${tm[@]}" split-window -dh -P -F '#{pane_id}' -l 50% -t "$p_a0" -c "$PWD" "bash -lc $(printf '%q' "$cmd1")" 9>&-)
    p_a2=$("${tm[@]}" split-window -dv -P -F '#{pane_id}' -l 50% -t "$p_a0" -c "$PWD" "bash -lc $(printf '%q' "$cmd2")" 9>&-)
    p_a3=$("${tm[@]}" split-window -dv -P -F '#{pane_id}' -l 50% -t "$p_a1" -c "$PWD" "bash -lc $(printf '%q' "$cmd3")" 9>&-)

    "${tm[@]}" set-option -p -t "$p_a0" @pane_label "GPU 0 | agent-0: Environment & Surface Inspector"
    "${tm[@]}" set-option -p -t "$p_a1" @pane_label "GPU 1 | agent-1: Socket & Protocol Matrix"
    "${tm[@]}" set-option -p -t "$p_a2" @pane_label "GPU 2 | agent-2: Compiled C Syscall Tester"
    "${tm[@]}" set-option -p -t "$p_a3" @pane_label "GPU 3 | agent-3: IPC & Coordinator"
    "${tm[@]}" set-option -p -t "$p_hud" @pane_label "eBPF-Jev Live LSM HUD"
    "${tm[@]}" select-pane -t "$p_a0"
else
    run=$(cat runs/.pi-swarm-current)
    qpid=$(cat "$run/qemu.pid" 2>/dev/null || true)
    mpid=$(cat "$run/mux.pid" 2>/dev/null || true)
fi
flock -u 9

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

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