#!/usr/bin/env bash
# DV-5, the fence on benchbox: sandbox_deciders.sh's bwrap form (DV-1 §7), moved to the bench box. The model's
# server AND the client run inside ONE network-less namespace (--unshare-net leaves only lo). The filesystem is
# read-only; /home is a tmpfs showing only the lane's scratch pieces below, read-only, except the rows, the
# receipts and the arm's scratch HOME. The guard's heartbeat folder is visible read-only (the runner reads it).
#
#   CPU arms: no card node is bound, CUDA_VISIBLE_DEVICES is empty, and the venv carries CPU-only torch
#             (2.14.1+cpu, the accuracy legs' lock), 8 threads; the caller pins CPUs 0-7 (one per core).
#   GPU arms: the CUDA device nodes are bound in; CUDA_VISIBLE_DEVICES is the pinned card's UUID, passed in
#             DV_CARD by the unit body and never written to a file; 7 threads and TORCH_DISABLE_NATIVE_JIT=1,
#             the house's form for every card-B row on exhibit sixty-six (O-2 amendment v1, L-2, A-1, I-2);
#             the caller pins CPUs 1-7.
#
#   usage: DV_DIR=<scratch> [DV_CARD=<uuid>] sandbox_speed.sh <speed-von-cpu|speed-laya-cpu|speed-von-gpu|speed-laya-gpu> <cmd...>
set -euo pipefail
ARM=$1; shift
: "${DV_DIR:?set DV_DIR to the scratch folder on the box}"
WT=$DV_DIR/wt
case "$ARM" in
  speed-von-cpu)  VENV=$DV_DIR/venv-von-cpu;    DEV=cpu ;;
  speed-laya-cpu) VENV=$DV_DIR/venv-laya-cpu;   DEV=cpu ;;
  speed-von-gpu)  VENV=$DV_DIR/venv-von-cu130;  DEV=cuda ;;
  speed-laya-gpu) VENV=$DV_DIR/venv-laya-cu130; DEV=cuda ;;
  *) echo "unknown arm $ARM" >&2; exit 2 ;;
esac
H=$DV_DIR/home-$ARM
mkdir -p "$H/tmp" "$DV_DIR/rows-speed" "$DV_DIR/rows-speed-dry" "$DV_DIR/receipts-speed" "$DV_DIR/guard"
cuda=(); extra=(); CVD=""; THREADS=8
if [ "$DEV" = cuda ]; then
  : "${DV_CARD:?a GPU arm needs DV_CARD}"
  for n in /dev/nvidia0 /dev/nvidiactl /dev/nvidia-uvm /dev/nvidia-uvm-tools; do
    [ -e "$n" ] && cuda+=(--dev-bind "$n" "$n")
  done
  CVD=$DV_CARD; THREADS=7
  extra=(--setenv TORCH_DISABLE_NATIVE_JIT 1 --setenv DV_CARD "$DV_CARD")
fi
hide=()
for d in /srv /opt /var /mnt /media /data; do [ -d "$d" ] && hide+=(--tmpfs "$d"); done
exec bwrap \
  --ro-bind / / \
  --tmpfs /home \
  --ro-bind "$WT" "$WT" --ro-bind "$VENV" "$VENV" --ro-bind "$DV_DIR/hf" "$DV_DIR/hf" \
  --ro-bind "$DV_DIR/python" "$DV_DIR/python" --ro-bind "$DV_DIR/guard" "$DV_DIR/guard" \
  --bind "$DV_DIR/rows-speed" "$DV_DIR/rows-speed" --bind "$DV_DIR/rows-speed-dry" "$DV_DIR/rows-speed-dry" \
  --bind "$DV_DIR/receipts-speed" "$DV_DIR/receipts-speed" --bind "$H" "$H" \
  "${hide[@]}" \
  --dev /dev --proc /proc --tmpfs /tmp --tmpfs /run \
  "${cuda[@]}" \
  --unshare-net --unshare-ipc --unshare-pid --unshare-uts \
  --die-with-parent --new-session --clearenv \
  --setenv HOME "$H" --setenv TMPDIR "$H/tmp" \
  --setenv PATH "$VENV/bin:/usr/bin:/bin" --setenv LANG C.UTF-8 \
  --setenv DV_DIR "$DV_DIR" --setenv HF_HOME "$DV_DIR/hf" \
  --setenv ORT_DISABLE_TELEMETRY 1 --setenv HF_HUB_DISABLE_TELEMETRY 1 \
  --setenv HF_HUB_OFFLINE 1 --setenv TRANSFORMERS_OFFLINE 1 \
  --setenv VLLM_NO_USAGE_STATS 1 --setenv GRADIO_ANALYTICS_ENABLED False \
  --setenv CUDA_VISIBLE_DEVICES "$CVD" --setenv USE_TF 0 \
  --setenv OMP_NUM_THREADS "$THREADS" --setenv MKL_NUM_THREADS "$THREADS" \
  --setenv TOKENIZERS_PARALLELISM false --setenv PYTHONDONTWRITEBYTECODE 1 \
  --setenv VON_DEVICE "$DEV" \
  --setenv LAYA_DEVICE "$DEV" --setenv LAYA_REVISION reviewed --setenv LAYA_MODELS english,multilingual \
  --setenv LAYA_PRELOAD 1 --setenv LAYA_THREADS "$THREADS" \
  "${extra[@]}" \
  -- "$@"
