#!/usr/bin/env bash # Start the whole production stack on the GPU server and run the e2e client against it, then stop it and check for leaks. # # leleka-inference (Original serve backend, sealed KAR memfd inherited from the holder) # leleka-daemon + leleka-cli load-mtp (explicit load; leleka-api only attaches) # leleka-api serve (runtime block) <- scripts/e2e/e2e_client.py # # MUST run inside a holder job (it needs LELEKA_STRA169_KAR_FD, a sealed memfd). It refuses to start otherwise: the model process # is never started outside the holder (MODEL.lock rules). Never builds anything. # # Environment (all optional except where noted): # E2E_BIN directory with leleka-inference leleka-daemon leleka-cli leleka-api (default ~/work/leleka-e2e/bin-sm120) # E2E_OUT output directory for logs and results.json (default ~/work/leleka-e2e/runs/) # E2E_CLIENT scripts/e2e/e2e_client.py (default next to this script) # E2E_CONTEXT directory whose .rs/.md files make the long prompt (default ~/work/leleka-e2e/context) # E2E_SCENARIOS client scenarios, comma separated or all (default all) # E2E_CLIENT_ARGS extra client arguments (e.g. "--soak-n 40 --burst 20") # E2E_ENGINE_ARGS extra leleka-inference arguments (e.g. "--original-prefill-capacity 4096") # E2E_ASSETS assets directory (default ~/leleka-gates/assets), e.g. ~/work/leleka/models/leleka-gates/assets # E2E_KAR_FILE path of the KAR archive as an ORDINARY READ-ONLY FILE (regular, one link, no write bits, owned by this user): the engine gets # --kar-archive-file instead of the holder's descriptor (the `file` KAR source, profile rtx5090-32g-file; nothing is copied to RAM). # No holder descriptor is needed or used, but MODEL.lock must still be held by the calling job. Add `--verify-stamps DIR` to # E2E_ENGINE_ARGS (DIR private, mode 0700) to skip the hash of the archive and the shards while their stamps are current. set -uo pipefail HERE=$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd) W=$HOME/work/leleka-e2e G=$HOME/work/gpu-runs A=${E2E_ASSETS:-/home/para/work/leleka/models/leleka-gates/assets} # shards, archive provenance, MTP pack, expert profile; E2E_ASSETS selects another copy of the same assets BIN=${E2E_BIN:-$W/bin-sm120} OUT=${E2E_OUT:-$W/runs/$(date -u +%Y%m%dT%H%M%SZ)} CLIENT=${E2E_CLIENT:-$HERE/e2e_client.py} CONTEXT=${E2E_CONTEXT:-$W/context} STATE=$W/state mkdir -p "$OUT" log() { echo "[run-e2e $(date -u +%T)] $*"; } # ---- guards ------------------------------------------------------------------------------------------------------- KARFILE=${E2E_KAR_FILE:-} FD=${LELEKA_STRA169_KAR_FD:-} if [ -n "$KARFILE" ]; then # file source: no descriptor; the file must be the form the engine admits (it checks again: regular, one link, owner, no write bits, pinned length) FD= if [ ! -f "$KARFILE" ] || [ "$(stat -c %h "$KARFILE")" != 1 ] || [ "$(( 0$(stat -c %a "$KARFILE") & 0222 ))" != 0 ]; then echo "refusing to start: E2E_KAR_FILE $KARFILE is not a single-link regular file without write bits (chmod 400)" >&2 exit 3 fi elif [ -z "$FD" ] || ! readlink "/proc/self/fd/$FD" 2>/dev/null | grep -q '^/memfd:'; then echo "refusing to start: LELEKA_STRA169_KAR_FD is not a sealed memfd inherited from the holder; run this as a holder job (or set E2E_KAR_FILE)" >&2 exit 3 fi CLOSE_FD=; [ -n "$FD" ] && CLOSE_FD="$FD>&-" KAR_ARG="--kar-archive-fd $FD"; [ -n "$KARFILE" ] && KAR_ARG="--kar-archive-file \"$KARFILE\"" [ -d "$G/MODEL.lock" ] || { echo "refusing to start: $G/MODEL.lock is not held" >&2; exit 3; } for b in leleka-inference leleka-daemon leleka-cli leleka-api; do [ -x "$BIN/$b" ] || { echo "missing $BIN/$b" >&2; exit 3; }; done if pgrep -u "$USER" -x 'leleka-inference|leleka-api|leleka-daemon' >/dev/null; then echo "refusing to start: a stack process of this user is already running" >&2; pgrep -a -u "$USER" -x 'leleka-inference|leleka-api|leleka-daemon' >&2; exit 3 fi if [ -n "$(nvidia-smi --query-compute-apps=pid --format=csv,noheader 2>/dev/null)" ]; then echo "refusing to start: something else already uses the GPU" >&2; nvidia-smi --query-compute-apps=pid,used_memory --format=csv,noheader >&2; exit 3 fi for p in 18766 18080 8081; do if ss -ltn "sport = :$p" 2>/dev/null | grep -q LISTEN; then echo "port $p is in use" >&2; exit 3; fi done # ---- environment -------------------------------------------------------------------------------------------------- export LD_LIBRARY_PATH=$HOME/cuda-toolkits/cublas-12.9.1.4/lib:$HOME/cuda-toolkits/13.2.2/lib64:${LD_LIBRARY_PATH:-} install -d -m 0700 "$STATE" "$G/mtp-staging-own" chmod 700 "$G/mtp-staging-own" umask 077 # two independent secrets and an API key; written to mode 0600 files and the environment, never printed mkrand() { head -c 36 /dev/urandom | base64 | tr -d '\n'; } export LELEKA_RUNTIME_TOKEN=$(mkrand) LELEKA_GATEWAY_TOKEN=$(mkrand) KEYFILE=$STATE/api-key rm -f "$KEYFILE" "$BIN/leleka-api" api-key init --file "$KEYFILE" >/dev/null 2>"$OUT/api-key-init.err" || { log "api-key init failed"; cat "$OUT/api-key-init.err"; exit 4; } chmod 600 "$KEYFILE" # engine tuning exactly as the 98x bring-up jobs (VRAM expert tier, placeholder profile); serve profile prints one line per request export LELEKA_ORIGINAL_SERVE_EXPERT_SLOTS=${LELEKA_ORIGINAL_SERVE_EXPERT_SLOTS:-2048} export LELEKA_ORIGINAL_SERVE_EXPERT_PER_LAYER=${LELEKA_ORIGINAL_SERVE_EXPERT_PER_LAYER:-yes} export LELEKA_ORIGINAL_SERVE_PCIE_FRAC=${LELEKA_ORIGINAL_SERVE_PCIE_FRAC:-0.45} # MTP draft attention = upstream pre75 chunk + merge v2 (bitwise identical to the Original): on in the rtx5090-32g profile's # engine_env, so on in the harness too (set LELEKA_SM120_ATTN_DAILY_MTP=0 for the old kernels; only the value 1 enables it). export LELEKA_SM120_ATTN_DAILY_MTP=${LELEKA_SM120_ATTN_DAILY_MTP:-1} # The ranked upstream profile (data/expert-profile.bin, the file crates/deploy pins as EXPERT_PROFILE_SHA256 and vanilla Strata runs) is the default. # The old default, $G/baseline/profile.bin, is a PLACEHOLDER in expert-id order (47 % overlap with the ranked set at 11,280 slots): every e2e run # that used it started from an unranked VRAM set, which showed up as a burst of CPU/PCIe experts in the first 20 windows of each request and an # 81 % instead of 97 % VRAM share (STRA-245 decode gap). The digest of the profile actually used is recorded in $OUT/expert-profile.sha256, and a digest # other than the pinned one is refused unless E2E_ALLOW_PROFILE_OVERRIDE=1 (then the record says so, and a paired comparison must not mix digests). PINNED_PROFILE_SHA256=8f59b4aa8873209dff11c11e37bcda9529a1335b724a1afeea37bf6388975baf export LELEKA_ORIGINAL_SERVE_EXPERT_PROFILE=${LELEKA_ORIGINAL_SERVE_EXPERT_PROFILE:-$A/data/expert-profile.bin} PROFILE_SHA256=$(sha256sum "$LELEKA_ORIGINAL_SERVE_EXPERT_PROFILE" 2>/dev/null | cut -d' ' -f1) if [ "$PROFILE_SHA256" = "$PINNED_PROFILE_SHA256" ]; then PROFILE_NOTE=pinned elif [ "${E2E_ALLOW_PROFILE_OVERRIDE:-}" = 1 ]; then PROFILE_NOTE=override log "WARNING: expert profile $LELEKA_ORIGINAL_SERVE_EXPERT_PROFILE has digest ${PROFILE_SHA256:-unreadable}, not the pinned $PINNED_PROFILE_SHA256 (E2E_ALLOW_PROFILE_OVERRIDE=1)" else echo "refusing to start: expert profile $LELEKA_ORIGINAL_SERVE_EXPERT_PROFILE has digest ${PROFILE_SHA256:-unreadable}, not the pinned $PINNED_PROFILE_SHA256; set E2E_ALLOW_PROFILE_OVERRIDE=1 to run on purpose (never in a paired comparison with the upstream profile)" >&2 exit 3 fi echo "profile=$LELEKA_ORIGINAL_SERVE_EXPERT_PROFILE sha256=$PROFILE_SHA256 pinned=$PINNED_PROFILE_SHA256 state=$PROFILE_NOTE" >"$OUT/expert-profile.sha256" export LELEKA_SERVE_PROFILE=1 LELEKA_GRAPH_EXEC_REUSE=off RUST_BACKTRACE=1 # synthetic prompts only: log the generated text when a streamed tool turn fails (operator debugging switch) export STRATA_DEBUG_FAILED_OUTPUT=1 cat >"$STATE/daemon.json" <"$STATE/strata.json" </dev/null; } stop_one() { # name pid grace local name=$1 pid=$2 grace=$3 alive "$pid" || { log "$name already gone"; return; } kill -INT "$pid" 2>/dev/null for _ in $(seq 1 "$grace"); do alive "$pid" || break; sleep 1; done if alive "$pid"; then log "LEAK: $name ($pid) ignored SIGINT for ${grace}s, killing"; kill -KILL "$pid" 2>/dev/null; echo "$name ignored SIGINT" >>"$OUT/defects.txt"; fi } cleanup() { log "shutting down" stop_one leleka-api "$PID_STR" 30 stop_one leleka-daemon "$PID_DAE" 30 stop_one leleka-inference "$PID_INF" 120 sleep 2 log "left over processes of this stack (expect none):" pgrep -a -u "$USER" -x 'leleka-inference|leleka-api|leleka-daemon' || echo " none" log "GPU compute apps (expect none):" nvidia-smi --query-compute-apps=pid,used_memory --format=csv,noheader || true nvidia-smi --query-gpu=memory.used --format=csv,noheader || true log "free -m: $(free -m | awk '/Mem:/{print "avail="$7" shared="$5}')" } trap cleanup EXIT # ---- tokenizer assets (offline, CPU) ------------------------------------------------------------------------------ if [ ! -d "$TOK/tokenizer" ]; then log "extracting tokenizer assets" "$BIN/leleka-api" model extract-tokenizer "$A/model/Qwen3.8-Flash-Next-GSQ-RCO-IQ3_S-00001-of-00002.gguf" --output "$TOK" >"$OUT/extract-tokenizer.log" 2>&1 || { log "extract-tokenizer failed"; tail -5 "$OUT/extract-tokenizer.log"; exit 4; } fi ls "$TOK" "$TOK/tokenizer" 2>&1 | head -5 # ---- engine ------------------------------------------------------------------------------------------------------ log "starting leleka-inference (sm: $(sha256sum "$BIN/leleka-inference" | cut -c1-16))" # The sealed KAR descriptor is inherited by the engine only: close it for the other processes started below. # Optional on-demand kernel tracing of the live engine (tools/cupti-trace of claude/ledger): the library is injected at process # start, idle until a trigger file appears. Not set -> nothing changes. if [ -n "${LELEKA_CUPTI_LIB:-}" ]; then [ -r "$LELEKA_CUPTI_LIB" ] || { echo "LELEKA_CUPTI_LIB not readable"; exit 3; } export CUDA_INJECTION64_PATH=$LELEKA_CUPTI_LIB LELEKA_CUPTI_TRIGGER=${LELEKA_CUPTI_TRIGGER:-$OUT/cupti.trigger} fi eval "\"$BIN/leleka-inference\" --listen 127.0.0.1:18766 --token-env LELEKA_RUNTIME_TOKEN \ --shard0 \"$A/model/Qwen3.8-Flash-Next-GSQ-RCO-IQ3_S-00001-of-00002.gguf\" \ --shard1 \"$A/model/Qwen3.8-Flash-Next-GSQ-RCO-IQ3_S-00002-of-00002.gguf\" \ --gpu 0 --gpu-budget ${E2E_GPU_BUDGET:-25769803776} --context-tokens 262144 \ $KAR_ARG --kar-provenance \"$A/qwen-iq3-s-l2-v4.kar.provenance.json\" \ --mtp-pack \"$A/data/mtp-rt\" --mtp-staging \"$G/mtp-staging-own\" \ --backend-mode original $RT_ARG ${E2E_ENGINE_ARGS:-} >\"$OUT/inference.log\" 2>&1 &" PID_INF=$! unset CUDA_INJECTION64_PATH sleep 3 alive "$PID_INF" || { log "leleka-inference died at startup"; tail -20 "$OUT/inference.log"; exit 5; } eval "\"$BIN/leleka-daemon\" --config \"$STATE/daemon.json\" $CLOSE_FD >\"$OUT/daemon.log\" 2>&1 &" PID_DAE=$! sleep 2 alive "$PID_DAE" || { log "leleka-daemon died at startup"; tail -20 "$OUT/daemon.log"; exit 5; } CLI() { "$BIN/leleka-cli" --config "$STATE/daemon.json" "$@"; } T_LOAD=$(date +%s) log "load-mtp: $(CLI load-mtp qwen-iq3 qwen4exp-kar-ple-mtp-v1 2>&1 | tr '\n' ' ' | sed 's/"operation_id":"[^"]*",//')" ready=0 for i in $(seq 1 480); do st=$(CLI status 2>&1) if echo "$st" | grep -Eq '"model_ready" *: *true'; then ready=1; break; fi alive "$PID_INF" || { log "leleka-inference died during load"; tail -30 "$OUT/inference.log"; exit 5; } if grep -q '^leleka-load phase=' "$OUT/inference.log" 2>/dev/null; then log "load failed:"; grep -E '^leleka-(load|serve)' "$OUT/inference.log" | head -10; echo "$st" | head -12; exit 5 fi sleep 5 done LOAD_S=$(( $(date +%s) - T_LOAD )) [ "$ready" = 1 ] || { log "model did not become ready in 2400 s"; echo "$st" | head -20; tail -30 "$OUT/inference.log"; exit 5; } log "model ready after ${LOAD_S}s" echo "load_seconds=$LOAD_S" >"$OUT/load.txt" # ---- API --------------------------------------------------------------------------------------------------------- eval "\"$BIN/leleka-api\" serve --config \"$STATE/strata.json\" $CLOSE_FD >\"$OUT/leleka-api.log\" 2>&1 &" PID_STR=$! if ! "$BIN/leleka-api" wait-ready --url http://127.0.0.1:8081 --model qwen-iq3 --key-file "$KEYFILE" --timeout-s 180 >"$OUT/wait-ready.log" 2>&1; then log "leleka-api never became ready"; tail -20 "$OUT/wait-ready.log"; tail -30 "$OUT/leleka-api.log"; exit 6 fi log "leleka-api ready" echo "pids inference=$PID_INF daemon=$PID_DAE strata=$PID_STR" rss() { awk '/VmRSS/{print int($2/1024)" MiB"}' /proc/$1/status; } log "rss after load: inference $(rss "$PID_INF"), leleka-api $(rss "$PID_STR"), gpu $(nvidia-smi --query-gpu=memory.used --format=csv,noheader)" # ---- client ------------------------------------------------------------------------------------------------------ if [ -n "${E2E_STATUS_FILE:-}" ]; then bash "$HERE/status_writer.sh" "$OUT" "$E2E_STATUS_FILE" "${E2E_STATUS_S:-60}" >/dev/null 2>&1 & PID_STATUS=$!; fi bash "$HERE/sample_env.sh" "$OUT/env.tsv" "${E2E_SAMPLE_S:-30}" "inference=$PID_INF" "strata=$PID_STR" "daemon=$PID_DAE" >/dev/null 2>&1 & PID_SAMPLER=$! if [ -n "${E2E_RUN_CMD:-}" ]; then # an external client (the Codex CLI jobs): it gets the base URL and a key variable; the key file stays private export E2E_BASE_URL=http://127.0.0.1:8081 E2E_MODEL=qwen-iq3 E2E_OUT_DIR="$OUT" E2E_API_KEY=$(cat "$KEYFILE") bash -c "$E2E_RUN_CMD" 2>&1 | tee "$OUT/client.log" RC=${PIPESTATUS[0]} else python3 -I "$CLIENT" --base http://127.0.0.1:8081 --key-file "$KEYFILE" --model qwen-iq3 --out "$OUT/results.json" \ --scenarios "${E2E_SCENARIOS:-all}" --context-dir "$CONTEXT" --pids "inference=$PID_INF,strata=$PID_STR,daemon=$PID_DAE" ${E2E_CLIENT_ARGS:-} 2>&1 | tee "$OUT/client.log" RC=${PIPESTATUS[0]} fi kill "$PID_SAMPLER" ${PID_STATUS:-} 2>/dev/null log "client exit status $RC" log "engine stderr tail (request outcome lines):" grep -E 'leleka-serve request|panicked|ERROR|error' "$OUT/inference.log" | tail -n 15 | cut -c1-300 log "leleka-api stderr (non-empty lines, head):" grep -v '^$' "$OUT/leleka-api.log" | head -n 15 | cut -c1-300 echo "$RC" >"$OUT/client.rc" exit "$RC"