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.

12.4 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=180
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_egress.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/pi-egress/egress_hud.py)
  --detached           Create the 5-pane tmux layout detached and print run dir
  --headless           Run all 4 pi.dev egress QA agents concurrently (--print), save logs & egress_report.md,
                       then stop the temporary KVM & isolated netns cleanly
  --timeout <sec>      Outer timeout for --headless mode (default: 180)
  --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-egress/kvm_egress_bridge.ts"
mux_script="$PWD/demo/pi-egress/egress_mux.py"
hud_script="$PWD/demo/pi-egress/egress_hud.py"
report_script="$PWD/demo/pi-egress/generate_report.py"
netns_launcher="$PWD/demo/pi-egress/netns_launcher.sh"
agent_dir="$PWD/demo/pi-egress/agent-dir"
roles_dir="$PWD/demo/pi-egress/roles"

for req in "$pi_bin" "$ext_path" "$mux_script" "$hud_script" "$report_script" "$netns_launcher" "$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=(
    "Direct Socket & Dual-Stack Egress QA"
    "DNS Egress & Label-Encoding QA"
    "Corporate HTTP Proxy & Corp API Boundary QA"
    "Kernel Transport, Syscall & Coordinator QA"
)

mkdir -p runs

start_airgap_kvm() {
    local run="$1"
    unshare -Urn bash "$netns_launcher" "$PWD" "$PWD/$run" "${run##*/}" >"$run/netns_launcher.log" 2>&1 9>&- &
    local npid=$!
    printf '%s\n' "$npid" > "$run/netns_wrapper.pid"
    for ((i=0; i<200; i++)); do
        kill -0 "$npid" 2>/dev/null || { echo "Air-gapped netns/QEMU exited early. See $run/netns_launcher.log and $run/qemu.log" >&2; return 1; }
        [[ -S "$run/shell.sock" && -e "$run/egress_services.ready" ]] && return 0
        sleep 0.05
    done
    echo "Timed out waiting for air-gapped QEMU 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 npid="$2"
    local mpid="$3"
    for ((i=0; i<1000; i++)); do
        [[ -e "$run/mux.ready" ]] && return 0
        if [[ -n "$npid" ]] && ! kill -0 "$npid" 2>/dev/null; then
            echo "Air-gapped KVM 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 "egress_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 npid qpid spid
    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
    npid=$(cat "$run/netns_wrapper.pid" 2>/dev/null || true)
    if [[ -n "$npid" ]] && kill -0 "$npid" 2>/dev/null; then
        for ((i=0; i<120; i++)); do
            kill -0 "$npid" 2>/dev/null || break
            sleep 0.1
        done
        if kill -0 "$npid" 2>/dev/null; then
            kill -TERM "$npid" 2>/dev/null || true
            sleep 0.5
            kill -KILL "$npid" 2>/dev/null || true
        fi
    fi
    for pfile in "$run/qemu.pid" "$run/egress_services.pid"; do
        pid=$(cat "$pfile" 2>/dev/null || true)
        if [[ -n "$pid" ]] && kill -0 "$pid" 2>/dev/null; then
            kill -TERM "$pid" 2>/dev/null || true
            sleep 0.2
            kill -KILL "$pid" 2>/dev/null || true
        fi
    done
}

if [[ $headless -eq 1 ]]; then
    if [[ -n "${JEV_RUN_NAME:-}" ]]; then
        run="runs/$JEV_RUN_NAME"
        mkdir -p "$run"
    else
        run="runs/$(date -u +%Y%m%dT%H%M%SZ)-pi-egress"
        mkdir -p "$run"
    fi
    printf '%s\n' "$run" > runs/.pi-egress-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-egress] Booting air-gapped Linux 6.12 KVM + isolated netns services in $run ..."
    start_airgap_kvm "$run"
    npid=$(cat "$run/netns_wrapper.pid")
    start_mux "$run"
    mpid=$(cat "$run/mux.pid")
    if ! wait_mux_ready "$run" "$npid" "$mpid"; then
        stop_run_vm "$run"
        exit 1
    fi
    echo "[pi-egress] Air-gapped KVM + mux.sock READY. Launching 4 parallel pi.dev egress QA agents (timeout=${timeout_sec}s)..."

    for i in 0 1 2 3; do
        role="${ROLE_NAMES[$i]}"
        sys_prompt="$roles_dir/agent-${i}.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-egress] started agent-${i} (gpu${i} :$((base_port + i)), pid=${agent_pids[$i]}, role=${role})"
    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-egress] agent-${i} finished (rc=${exit_codes[$i]})"
    done

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

    python3 "$hud_script" "$PWD/$run" --snapshot > "$run/hud.txt" 2>/dev/null || true

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

    stop_run_vm "$run"
    python3 "$report_script" "$PWD/$run" --model "$model" --timeout "$timeout_sec"
    echo "[pi-egress] Complete. Report: $run/egress_report.md"
    exit 0
fi

# Interactive / detached 5-pane tmux mode
socket="$PWD/runs/.pi-egress.tmux"
tm=(tmux -S "$socket")
exec 9>runs/.pi-egress-launch.lock
flock 9

if ! "${tm[@]}" has-session -t =jev-egress 2>/dev/null; then
    if [[ -n "${JEV_RUN_NAME:-}" ]]; then
        run="runs/$JEV_RUN_NAME"
        mkdir -p "$run"
    else
        run="runs/$(date -u +%Y%m%dT%H%M%SZ)-pi-egress"
        mkdir -p "$run"
    fi
    printf '%s\n' "$run" > runs/.pi-egress-current
    start_airgap_kvm "$run"
    npid=$(cat "$run/netns_wrapper.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}.md"
        local mission
        mission=$(<"$roles_dir/agent-${idx}.mission.txt")
        cat <<PANE
printf "Booting air-gapped 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_egress.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 egress arena in %s...\n" $(printf '%q' "$run")
while [[ ! -e $(printf '%q' "$PWD/$run/ready") ]]; do sleep 0.1; done
exec python3 demo/pi-egress/egress_hud.py $(printf '%q' "$PWD/$run")
HUD
)

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

    p_hud=$("${tm[@]}" split-window -dh -P -F '#{pane_id}' -l 40% -t "$p_a0" -c "$PWD" "bash -lc $(printf '%q' "$hud_cmd")" 9>&-)
    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: Direct Sockets & Dual-Stack QA"
    "${tm[@]}" set-option -p -t "$p_a1" @pane_label "GPU 1 | agent-1: DNS Egress & Tunneling QA"
    "${tm[@]}" set-option -p -t "$p_a2" @pane_label "GPU 2 | agent-2: Corporate Proxy & API Boundary QA"
    "${tm[@]}" set-option -p -t "$p_a3" @pane_label "GPU 3 | agent-3: Kernel Transport & Coordinator QA"
    "${tm[@]}" set-option -p -t "$p_hud" @pane_label "eBPF-Jev Offline Egress Live HUD"
    "${tm[@]}" select-pane -t "$p_a0"
else
    run=$(cat runs/.pi-egress-current)
    npid=$(cat "$run/netns_wrapper.pid" 2>/dev/null || true)
    mpid=$(cat "$run/mux.pid" 2>/dev/null || true)
fi
flock -u 9

if ! wait_mux_ready "$run" "${npid:-}" "${mpid:-}"; then
    "${tm[@]}" kill-session -t =jev-egress 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-egress