Skip to content

The first fruit of the Nüwa tree — an autonomous AI pentest legion (634-team champion, 8h unattended, 1B tokens beat 4.5B). Not just a tool: evolvable by design. Grow your own fruit on the Nüwa Foundation (Create/Language/History).

14.9 KB
Raw Blame
#!/usr/bin/env bash
# tmux-drive.sh v2 — self-healing driver for the TUI-mode agent fleet.
#
# Failure modes handled:
#   - Enter lost / mission stuck in the input box  -> re-Enter (3 tries), then restart window
#   - trust/bypass/permission dialogs              -> auto-accept (Down+Enter)
#   - claude TUI exits / pane dead                 -> relaunch window (max 2/challenge)
#   - agent stuck (no pane change)                 -> nudge; still stuck at budget -> stop
#   - driver restart                               -> adopts existing tmux windows (idempotent)
#   - challenge DONE or per-challenge timeout      -> submit recorded flags, close, next
set -o pipefail

ROOT="${SAPPER_ROOT:-/work/runs}"
LOG="$ROOT/drive.log"
MODEL="${SAPPER_MODEL:-glm-5.3-flash[1m]}"
PARALLEL="${SAPPER_PARALLEL:-6}"
TOTAL_MIN="${SAPPER_TOTAL_MINUTES:-470}"
PER_MIN="${SAPPER_MINUTES_PER_CHALLENGE:-25}"
STALL_SECONDS="${SAPPER_STALL_SECONDS:-300}"
SESSION=sapper-tui
MAX_RESTARTS=2
mkdir -p "$ROOT"
log() { printf '[drive] %s\n' "$*" | tee -a "$LOG"; }
END_AT=$(( $(date +%s) + TOTAL_MIN * 60 ))
NOW() { date +%s; }

declare -A launch_at last_activity restarts finished claim_start last_pane_state

# ---- arena helper (graph-free) ----------------------------------------------
arena() {
  python3 - "$@" <<'PYEOF2'
import importlib.util, json, sys
spec = importlib.util.spec_from_file_location("arena", "/opt/sapper/lib/arena.py")
m = importlib.util.module_from_spec(spec); spec.loader.exec_module(m)
c = m.ArenaClient.load(None)
op = sys.argv[1]
if op == "list":
    print(json.dumps(c.list_challenges()))
elif op == "start":
    print(json.dumps(c.start_challenge(sys.argv[2])))
elif op == "close":
    print(json.dumps(c.close_challenge(sys.argv[2])))
PYEOF2
}

# ---- key-pool rotation ------------------------------------------------------
# SAPPER_KEY_POOL: JSON [{base_url,token,model},...]. On quota/balance/auth
# errors the driver rotates its own env; windows relaunched afterwards
# inherit the new key, and `claude --continue` restores the prior session.
rotate_key() {
  python3 - <<'PYROT'
import json, os
raw = os.environ.get("SAPPER_KEY_POOL", "").strip()
try:
    pool = [p for p in json.loads(raw) if isinstance(p, dict) and p.get("token")]
except Exception:
    pool = []
if not pool:
    raise SystemExit(1)
cur = os.environ.get("ANTHROPIC_AUTH_TOKEN", "")
start = next((i for i, e in enumerate(pool) if e["token"] == cur), -1)
nxt = pool[(start + 1) % len(pool)]
if start >= 0 and nxt["token"] == cur:
    raise SystemExit(1)  # single key: nothing to rotate to
for k, v in (
    ("ANTHROPIC_BASE_URL", nxt.get("base_url")),
    ("ANTHROPIC_AUTH_TOKEN", nxt["token"]),
    ("SAPPER_MODEL", nxt.get("model")),
):
    if v:
        os.environ[k] = v
with open(os.environ.get("SAPPER_ROOT", "/work/runs") + "/rotated.env", "w") as fh:
    fh.write(
        f'export ANTHROPIC_BASE_URL="{os.environ["ANTHROPIC_BASE_URL"]}"\n'
        f'export ANTHROPIC_AUTH_TOKEN="{os.environ["ANTHROPIC_AUTH_TOKEN"]}"\n'
        f'export SAPPER_MODEL="{os.environ.get("SAPPER_MODEL", "")}"\n'
    )
raise SystemExit(0)
PYROT
  local rc=$?
  if (( rc == 0 )) && [[ -f "$ROOT/rotated.env" ]]; then
    # shellcheck disable=SC1090
    . "$ROOT/rotated.env"; export ANTHROPIC_BASE_URL ANTHROPIC_AUTH_TOKEN SAPPER_MODEL
    log "key rotated -> $ANTHROPIC_BASE_URL (model $SAPPER_MODEL)"
    return 0
  fi
  return 1
}

QUOTA_RE='insufficient|balance|quota|exceeded|billing|payment|401|[Uu]nauthorized|invalid.{0,12}[Aa][Pp][Ii].[Kk]ey|欠费|余额'

# submit every recorded-unsubmitted flag fact in a challenge's graph
finish_flags() {  # finish_flags <code>
  python3 - "$1" <<'PYFIN'
import glob, json, os, sys, importlib.util
code = sys.argv[1]
root = os.environ.get("SAPPER_ROOT", "/work/runs")
spec = importlib.util.spec_from_file_location("arena", "/opt/sapper/lib/arena.py")
m = importlib.util.module_from_spec(spec); spec.loader.exec_module(m)
try:
    client = m.ArenaClient.load(None)
except Exception as exc:
    print(f"[finish] {code}: no arena ({exc})"); sys.exit(0)
for path in sorted(glob.glob(f"{root}/{code}/.sapper/facts/*.json")):
    try:
        fact = json.load(open(path))
    except Exception:
        continue
    if fact.get("kind") != "flag":
        continue
    data = fact.get("data") or {}
    if data.get("status") not in (None, "recorded") or not data.get("value"):
        continue
    try:
        r = client.submit_flag(code, data["value"])
        fact["data"]["status"] = "submitted" if r.get("correct") else "rejected"
        json.dump(fact, open(path, "w"), ensure_ascii=False, indent=2)
        print(f"[finish] {code}: submit {data['value'][:24]}… correct={r.get('correct')}")
    except Exception as exc:
        print(f"[finish] {code}: submit failed ({exc})")
PYFIN
}

finish_challenge() {  # finish_challenge <code> <reason>
  local code="$1" reason="$2"
  [[ -n "${finished[$code]:-}" ]] && return 0
  finished[$code]=1
  log "$code: finishing ($reason)"
  finish_flags "$code" 2>&1 | tee -a "$LOG"
  arena close "$code" >/dev/null 2>&1 && log "$code: container closed"
  tmux kill-window -t "$SESSION:$code" 2>/dev/null
}

# ---- launch / adopt ---------------------------------------------------------
pane_text() { tmux capture-pane -pt "$SESSION:$1" 2>/dev/null; }

send_literal() {  # send_literal <window> <text> [enter]
  tmux send-keys -t "$SESSION:$1" -l -- "$2"
  sleep 0.5
  [[ "${3:-enter}" == "enter" ]] && tmux send-keys -t "$SESSION:$1" Enter
}

accept_dialogs() {  # accept_dialogs <window> <pane> -> 0 if a dialog was dismissed
  local win="$1" pane="$2"
  if printf '%s' "$pane" | grep -q 'Enter to confirm'; then
    tmux send-keys -t "$SESSION:$win" Down; sleep 0.4
    tmux send-keys -t "$SESSION:$win" Enter; sleep 1.5
    return 0
  fi
  return 1
}

launch() {  # launch <code>
  local code="$1"
  local err; err=$(mktemp)
  local started; started=$(arena start "$code" 2>"$err" || true)
  LAST_LAUNCH_ERR="$(cat "$err" 2>/dev/null || true)"
  cat "$err" >>"$LOG" 2>/dev/null || true; rm -f "$err"
  local addrs; addrs=$(printf '%s' "$started" | python3 -c '
import json,sys
try: print(" ".join(json.load(sys.stdin).get("container_addr") or []))
except Exception: pass' 2>/dev/null)
  log "$code: start -> ${addrs:-FAILED}"
  [[ -z "$addrs" ]] && return 1
  mkdir -p "$ROOT/$code/.claude"
  (cd "$ROOT/$code" && sapper init --goal "solve $code, capture all flags" >/dev/null 2>&1 || true)
  printf '%s\n' '{"permissions":{"defaultMode":"bypassPermissions"}}' > "$ROOT/$code/.claude/settings.json"
  python3 - "$ROOT/$code" <<'PYTRUST'
import json, os, sys
path = os.path.expanduser("~/.claude.json")
try: d = json.load(open(path))
except Exception: d = {}
d.setdefault("projects", {})[sys.argv[1]] = {
    "hasTrustDialogAccepted": True, "bypassPermissionsModeAccepted": True}
json.dump(d, open(path, "w"))
PYTRUST
  tmux kill-window -t "$SESSION:$code" 2>/dev/null
  local resume_flag=""
  ls "$HOME/.claude/projects/"*"$code"*/  >/dev/null 2>&1 && resume_flag="--continue"
  tmux new-window -d -t "$SESSION" -n "$code" \
    "cd $ROOT/$code && claude $resume_flag --model '$MODEL' --dangerously-skip-permissions 2>&1; echo DRIVE_PANE_EXITED; exec sleep infinity"
  launch_at[$code]=$(NOW); last_activity[$code]=$(NOW)
  restarts[$code]=$(( ${restarts[$code]:-0} ))
  claim_start[$code]=0

  # wait for the input box, auto-accepting dialogs
  local waited=0 pane=""
  while (( waited < 75 )); do
    pane=$(pane_text "$code")
    accept_dialogs "$code" "$pane" && { waited=$((waited+3)); continue; }
    printf '%s' "$pane" | grep -q '❯' && break
    sleep 1; waited=$((waited+1))
  done
  printf '%s' "$pane" | grep -q '❯' || { log "$code: input box never appeared"; return 1; }

  local mission
  mission="You are an autonomous pentest operator in a CTF benchmark. Challenge: $code. Target container addresses (reach them directly): ${addrs}. Mission: recon the target, find the vulnerabilities, capture EVERY flag, and for each flag IMMEDIATELY run: sapper flag \"flag{...}\" -c $code --submit  (this submits to the scoring platform and prints the result). Record findings as typed facts: sapper fact \"...\" -k service -d host=... -d port=...  Consult the knowledge base when unsure: sapper kb search \"keywords\". Multiple flags are likely (${PER_MIN}min budget) — after one submission keep hunting the rest. Work fully autonomously, never ask questions. When totally done, reply with the single word DONE."
  send_literal "$code" "$mission"
  sleep 3
  # verify: mission text visible in the input box, OR agent already working
  pane=$(pane_text "$code")
  if ! printf '%s' "$pane" | grep -q 'autonomous pentest operator\|esc to interrupt'; then
    log "$code: mission not confirmed in pane — retrying once"
    send_literal "$code" "$mission"
    sleep 3
  fi
  last_activity[$code]=$(NOW)
  log "$code: TUI launched, mission sent"
  return 0
}

adopt_existing() {
  local win
  for win in $(tmux list-windows -t "$SESSION" -F '#W' 2>/dev/null | grep -v '^supervisor$'); do
    [[ -n "${launch_at[$win]:-}" ]] && continue
    launch_at[$win]=$(NOW); last_activity[$win]=$(NOW); restarts[$win]=0
    log "$win: adopted existing window after driver restart"
  done
}

list_pending() {
  arena list 2>>"$LOG" | python3 -c '
import json,sys
try: items=json.load(sys.stdin)
except Exception: sys.exit(0)
for c in items:
    if isinstance(c,dict) and not c.get("is_completed"): print(c["unique_code"])' 2>/dev/null
}

# ---- build the pending queue -------------------------------------------------
mapfile -t ALL_CODES < <(list_pending)
log "challenges pending: ${ALL_CODES[*]:-none} (parallel=$PARALLEL, per=$PER_MIN min, total=$TOTAL_MIN min)"
idx=0
adopt_existing

# ---- main loop ---------------------------------------------------------------
while (( $(NOW) < END_AT )); do
  active_windows=$(tmux list-windows -t "$SESSION" -F '#W' 2>/dev/null | grep -v '^supervisor$' || true)
  active_count=$(printf '%s\n' "$active_windows" | grep -c . || true)

  # top up to PARALLEL with not-yet-started codes
  while (( ${active_count:-0} < PARALLEL && idx < ${#ALL_CODES[@]} )); do
    code="${ALL_CODES[idx]}"
    idx=$((idx+1))
    if [[ -n "${finished[$code]:-}" ]]; then continue; fi
    if printf '%s\n' "$active_windows" | grep -qx "$code"; then continue; fi
    if (( ${restarts[$code]:-0} > MAX_RESTARTS )); then
      log "$code: exceeded restart budget, parking"
      continue
    fi
    launch "$code" && active_count=$((active_count+1)) || {
      if printf '%s' "${LAST_LAUNCH_ERR:-}" | grep -qi "max active"; then
        # platform cap — not this challenge's fault: requeue it and stop
        # burning through the list (a full sweep marks everything failed
        # and exhausts idx, after which top-up never runs again).
        idx=$((idx-1))
        log "$code: platform active cap — requeued, waiting for a free slot"
        break
      fi
      restarts[$code]=$((${restarts[$code]:-0}+1))
      log "$code: launch failed (restart ${restarts[$code]}/$MAX_RESTARTS)"
    }
    sleep 3
  done

  for win in $active_windows; do
    pane=$(pane_text "$win")
    # 1) any confirm dialog -> accept
    if accept_dialogs "$win" "$pane"; then
      last_activity[$win]=$(NOW); continue
    fi
    # 2) pane dead (claude exited)
    if printf '%s' "$pane" | grep -q 'DRIVE_PANE_EXITED'; then
      restarts[$win]=$(( ${restarts[$win]:-0} + 1 ))
      if (( ${restarts[$win]} > MAX_RESTARTS )); then
        finish_challenge "$win" "TUI exited, no restarts left"
      else
        log "$win: TUI exited — relaunching (${restarts[$win]}/$MAX_RESTARTS)"
        tmux kill-window -t "$SESSION:$win" 2>/dev/null
        launch "$win" || true
      fi
      continue
    fi
    # 3) mission text sitting unsubmitted in the input box -> re-Enter
    if printf '%s' "$pane" | grep -q '❯.*autonomous pentest operator'; then
      claim_start[$win]=$((${claim_start[$win]:-0}+1))
      if (( ${claim_start[$win]} > 3 )); then
        log "$win: mission never submits — restarting window"
        tmux kill-window -t "$SESSION:$win" 2>/dev/null
        launch "$win" || true
      else
        log "$win: mission in input box unsubmitted — re-Enter (${claim_start[$win]}/3)"
        tmux send-keys -t "$SESSION:$win" Enter
        sleep 2
      fi
      last_activity[$win]=$(NOW); continue
    fi
    # 3.5) quota/auth exhaustion in the pane -> rotate key, relaunch with resume
    if printf '%s' "$pane" | grep -qE "$QUOTA_RE"; then
      if rotate_key; then
        log "$win: quota/auth error in pane — key rotated, relaunching (session resumes)"
        restarts[$win]=${restarts[$win]:-0}   # rotation is not the agent's fault: don't burn restarts
        tmux kill-window -t "$SESSION:$win" 2>/dev/null
        launch "$win" || true
      else
        log "$win: quota/auth error and no spare key — nudging later"
      fi
      last_activity[$win]=$(NOW); continue
    fi
    # 4) activity tracking + stall nudge
    if [[ -n "$pane" && "$pane" != "${last_pane_state[$win]:-}" ]]; then
      last_pane_state[$win]="$pane"; last_activity[$win]=$(NOW)
    else
      idle=$(( $(NOW) - ${last_activity[$win]:-$(NOW)} ))
      if (( idle > STALL_SECONDS )); then
        log "$win: stalled ${idle}s — nudging"
        send_literal "$win" "Continue autonomously. If stuck, change approach or hunt the next flag. Ensure every captured flag was submitted via sapper flag. Reply DONE only when finished."
        last_activity[$win]=$(NOW)
      fi
    fi
    # 5) DONE detection -> finish and free the slot
    if printf '%s' "$pane" | tail -60 | grep -q '\bDONE\b'; then
      sleep 8   # let trailing flag submissions land
      finish_challenge "$win" "agent reported DONE"
      continue
    fi
    # 6) per-challenge budget
    if (( $(NOW) - ${launch_at[$win]:-$(NOW)} > (PER_MIN * 60 + 120) )); then
      tmux send-keys -t "$SESSION:$win" C-c 2>/dev/null; sleep 2
      finish_challenge "$win" "per-challenge budget exhausted"
      continue
    fi
  done

  # all challenges finished? (re-read windows: top-up may have changed them)
  active_windows=$(tmux list-windows -t "$SESSION" -F '#W' 2>/dev/null | grep -v '^supervisor$' || true)
  if (( idx >= ${#ALL_CODES[@]} )) && [[ -z "$active_windows" ]]; then
    left=$(arena list 2>>"$LOG" | python3 -c '
import json,sys
try: items=json.load(sys.stdin)
except Exception: print(999); sys.exit()
print(sum(1 for c in items if not c.get("is_completed")))' 2>/dev/null || echo 999)
    if (( ${left:-999} == 0 )); then
      log "all challenges complete"; break
    else
      log "platform still shows ${left:-?} unfinished — refreshing queue"
      mapfile -t ALL_CODES < <(list_pending)
      idx=0
      for k in "${!finished[@]}"; do unset "finished[$k]"; done
    fi
  fi
  sleep 15
done

log "deadline reached — wrapping up"
for win in $(tmux list-windows -t "$SESSION" -F '#W' 2>/dev/null | grep -v '^supervisor$'); do
  tmux send-keys -t "$SESSION:$win" C-c 2>/dev/null
done
sleep 3
for win in $(tmux list-windows -t "$SESSION" -F '#W' 2>/dev/null | grep -v '^supervisor$'); do
  finish_challenge "$win" "deadline"
done
log "drive complete"