#!/usr/bin/env bash
# Copyright 2026 Anthropic PBC
# SPDX-License-Identifier: Apache-2.0
# Run vuln-pipeline — or dnr-pipeline: pass it as the first arg — with the
# agent sandbox active. Verifies runsc + the egress proxy are up, then sets
# the env the pipelines read (sandbox.py) and execs through. Everything else
# — spawning the per-agent gVisor containers — happens inside the pipeline.
set -euo pipefail

REPO_ROOT="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)"

BIN=vuln-pipeline
case "${1:-}" in
    vuln-pipeline|dnr-pipeline) BIN=$1; shift ;;
    # a path here looks right but would fall through to vuln-pipeline as a
    # bogus subcommand — the venv entry point is resolved below, not by $1
    */vuln-pipeline|*/dnr-pipeline)
        echo "error: pass the bare name '${1##*/}', not a path — vp-sandboxed resolves the venv entry point itself" >&2
        exit 2 ;;
esac
# entry points are generated at install time — a venv from before this
# binary was added won't have it until pip install -e . runs again
[ -x "$REPO_ROOT/.venv/bin/$BIN" ] \
    || { echo "error: $REPO_ROOT/.venv/bin/$BIN not found — run .venv/bin/pip install -e . to register it" >&2; exit 1; }
NET=${VULN_PIPELINE_AGENT_NETWORK:-vp-internal}
PROXY_NAME=vp-egress-proxy
RUNTIME=${VULN_PIPELINE_AGENT_RUNTIME:-runsc}

docker info --format '{{range $k,$v := .Runtimes}}{{$k}} {{end}}' \
    | tr ' ' '\n' | grep -qx "$RUNTIME" \
    || { echo "error: runtime '$RUNTIME' not registered; run scripts/setup_sandbox.sh" >&2; exit 1; }

# docker inspect exits 0 even when the container isn't on $NET — the Go
# template just renders "<no value>". Guard against that and empty output
# explicitly, not just inspect's exit code.
proxy_ip=$(docker inspect "$PROXY_NAME" --format \
    '{{(index .NetworkSettings.Networks "'"$NET"'").IPAddress}}' 2>/dev/null) || proxy_ip=""
if [ -z "$proxy_ip" ] || [ "$proxy_ip" = "<no value>" ]; then
    echo "error: ${PROXY_NAME} not running on ${NET}; run scripts/setup_sandbox.sh" >&2
    exit 1
fi

export VULN_PIPELINE_AGENT_RUNTIME="$RUNTIME"
export VULN_PIPELINE_AGENT_NETWORK="$NET"
export VULN_PIPELINE_EGRESS_PROXY="http://${proxy_ip}:3128"

PROXY_ALLOW=$(docker inspect "$PROXY_NAME" --format \
    '{{range .Config.Env}}{{println .}}{{end}}' | sed -n 's/^VP_EGRESS_ALLOW=//p')
"$REPO_ROOT/.venv/bin/python3" -c \
    'import sys; from harness.auth import check_egress_satisfied; check_egress_satisfied(sys.argv[1])' \
    "$PROXY_ALLOW" \
    || { echo "  → re-run scripts/setup_sandbox.sh with current provider env set" >&2; exit 1; }

exec "$REPO_ROOT/.venv/bin/$BIN" "$@"
