#!/usr/bin/env bash
# Run any agent harness as one specific Scio agent.
#   scio-as <alias> <command...>      export SCIO_API_KEY for <alias>, then exec the command
#   scio-as <alias> --print-env       print the export lines (for harnesses configured through a settings UI)
#   scio-as <alias> --supervise <command...>   run the command and restart it after harness usage limits (supervise.py)
#   scio-as <alias> --supervise --watch <command...>   unattended: start the command (one round) only when scio.md has work
#                                     for this agent — the waiting costs no model call. Options: --poll 300 --tasks-every 60
#                                     --for 8h --max-rounds N --max-restarts N --log FILE (supervise.py explains each)
#   scio-as --list                    list the aliases in the keys file
# Keys come from $SCIO_KEYS_FILE or ./scio/key/keys (written by register-models.py): one "alias=key" per line.
# Examples:
#   scio-as opus   claude --model opus
#   scio-as sonnet claude --model sonnet -p "review my Scio assignments"
#   scio-as gpt5   codex
#   scio-as gemini gemini
#   scio-as haiku  opencode
#   scio-as opus   --supervise --watch claude --model opus -p "/scio:loop --once"
#   eval "$(scio-as fable --print-env)"; cursor .
set -euo pipefail
here="$(dirname "$(readlink -f "$0" 2>/dev/null || echo "$0")")"   # the skill's scripts/, through a symlink on the PATH
# The keys file read by the servers' own code (scio_common): the same file, the same parsing, the same refusals (a link, a
# checkout's copy) — otherwise a launch under scio-as and a session without it sign with two different keys.
keys="$(python3 -c 'import sys; sys.path.insert(0, sys.argv[1]); from scio_common import keys_path; print(keys_path())' "$here" 2>/dev/null || echo ./scio/key/keys)"
entries() { python3 -c 'import sys; sys.path.insert(0, sys.argv[1]); from scio_common import read_keys
for alias, key in read_keys()[0].items(): print(alias + "\t" + key)' "$here" 2>/dev/null || true; }
list() { entries | awk -F'\t' '!seen[$1]++ { printf "%s ", $1 }'; echo; }
case "${1:-}" in
  ""|-h|--help) sed -n '2,18p' "$0" | sed 's/^# \{0,1\}//' >&2; exit 2 ;;
  --list) list; exit 0 ;;
esac
alias_="$1"; shift
case "$alias_" in *[!A-Za-z0-9_-]*) echo "scio: alias '$alias_' may contain only letters, digits, '_' and '-'" >&2; exit 2 ;; esac
key="$(entries | awk -F'\t' -v a="$alias_" '$1 == a { v = $0; sub(/^[^\t]*\t/, "", v); key = v } END { print key }' || true)"
if [ -z "$key" ]; then
  echo "scio: no key for '$alias_' in $keys (have: $(list)). Run register-models.py --models ${alias_}=<model_version> first." >&2
  exit 1
fi
if [ "${1:-}" = "--print-env" ]; then
  harness="${SCIO_HARNESS:-unknown}"
  printf 'export SCIO_API_KEY=%q\nexport SCIO_AGENT=%q\nexport SCIO_HARNESS=%q\n' "$key" "$alias_" "$harness"
  exit 0
fi
if [ $# -eq 0 ]; then
  echo "scio: give the harness command to run, e.g. scio-as $alias_ claude --model $alias_" >&2
  exit 2
fi
if [ "${1:-}" = "--supervise" ]; then
  shift
  sup_opts=()   # the supervisor's own options come before the harness command, which never starts with one of them
  while [ $# -gt 0 ]; do
    case "$1" in
      --watch) sup_opts+=("$1"); shift ;;
      --poll|--tasks-every|--for|--max-rounds|--max-restarts|--log)
        [ $# -ge 2 ] || { echo "scio: $1 needs a value" >&2; exit 2; }
        sup_opts+=("$1" "$2"); shift 2 ;;
      *) break ;;
    esac
  done
  if [ $# -eq 0 ]; then
    echo "scio: give the harness command after --supervise, e.g. scio-as $alias_ --supervise --watch claude -p \"/scio:loop --once\"" >&2
    exit 2
  fi
  export SCIO_API_KEY="$key" SCIO_AGENT="$alias_" SCIO_HARNESS="${SCIO_HARNESS:-$(basename "$1")}"
  sup="$here/supervise.py"
  if [ ! -f "$sup" ]; then
    echo "scio: supervise.py is not next to this scio-as ($sup); install scio-as as a symlink to the skill's scripts/scio-as, not a copy" >&2
    exit 1
  fi
  exec python3 "$sup" ${sup_opts[@]+"${sup_opts[@]}"} -- "$@"   # the ${x[@]+…} form: an empty array under `set -u` is an error before bash 4.4 (macOS)
fi
# SCIO_AGENT names the alias whose key this is: the key decides who the agent is, the alias lets the skill say so
# (whoami.py keeps its reminder record per alias) — and it replaces a stale SCIO_AGENT left in the environment.
export SCIO_API_KEY="$key" SCIO_AGENT="$alias_" SCIO_HARNESS="${SCIO_HARNESS:-$(basename "$1")}"
exec "$@"
