#!/usr/bin/env bash
# In-container entrypoint for one harness seat. Seeds credentials from the read-only
# /seed mount into the tmpfs HOME, then execs the WRAPPER-CONTRACT wrapper unchanged.
#
# Exit codes are the wrapper's own — this file never invents one except 2 (its own
# usage error). exec means the wrapper's rc is the container's rc.
set -uo pipefail

WRAPPER="" WORKSPACE="" TASK_SLUG="" MODEL="" TIMEOUT="" PROFILE="" PERMISSION_MODE="" RESUME="" THREAD_ID="" LOG_DIR=""
while [[ $# -gt 0 ]]; do
  case "$1" in
    --wrapper)   WRAPPER="${2:-}";   shift 2;;
    --workspace) WORKSPACE="${2:-}"; shift 2;;
    --task-slug) TASK_SLUG="${2:-}"; shift 2;;
    --model)     MODEL="${2:-}";     shift 2;;
    --timeout)   TIMEOUT="${2:-}";   shift 2;;
    --profile)   PROFILE="${2:-}";   shift 2;;
    --permission-mode)
      [[ $# -ge 2 && -n "${2:-}" && -z "$PERMISSION_MODE" ]] \
        || { echo '{"ok":false,"detail":"seat-entrypoint: --permission-mode requires one value"}' >&2; exit 2; }
      PERMISSION_MODE="$2"; shift 2;;
    --resume)    RESUME="${2:-}";    shift 2;;
    --thread-id) THREAD_ID="${2:-}"; shift 2;;
    --log-dir)   LOG_DIR="${2:-}";   shift 2;;
    *) echo "{\"ok\":false,\"detail\":\"seat-entrypoint: unknown arg $1\"}" >&2; exit 2;;
  esac
done

[[ -n "$TASK_SLUG" ]] || { echo '{"ok":false,"detail":"seat-entrypoint: --task-slug required"}' >&2; exit 2; }
[[ -z "$PERMISSION_MODE" || "$PERMISSION_MODE" == "safe" ]] || { echo '{"ok":false,"detail":"seat-entrypoint: --permission-mode safe required"}' >&2; exit 2; }
[[ "$TASK_SLUG" != incident-* || "$PERMISSION_MODE" == "safe" ]] || { echo '{"ok":false,"detail":"seat-entrypoint: incident dispatch requires --permission-mode safe"}' >&2; exit 2; }
[[ -x "$WRAPPER" || -f "$WRAPPER" ]] || { echo '{"ok":false,"detail":"seat-entrypoint: wrapper missing in workspace mount"}' >&2; exit 2; }
[[ -d "$WORKSPACE" ]] || { echo '{"ok":false,"detail":"seat-entrypoint: workspace missing"}' >&2; exit 2; }
[[ -f /seed/prompt.txt ]] || { echo '{"ok":false,"detail":"seat-entrypoint: /seed/prompt.txt missing"}' >&2; exit 2; }

# Its own tmpfs, not a subdir of /tmp: codex refuses to create its PATH helper binaries when
# CODEX_HOME resolves under a temporary directory, and warns on every run.
export HOME=/seat-home
mkdir -p "$HOME" || { echo '{"ok":false,"detail":"seat-entrypoint: HOME tmpfs not writable"}' >&2; exit 2; }
chmod 0700 "$HOME"
# claude runs as uid 0 in here and refuses --permission-mode bypassPermissions without this.
export IS_SANDBOX=1

# Credentials are copied, never bind-mounted writable: a refreshed token stays inside
# this container and dies with it, so a seat can never rewrite the host's credentials.
seed_copy() {
  local src="$1" dest="$2"
  [[ -f "$src" ]] || return 0
  mkdir -p "$(dirname "$dest")" && install -m 0600 "$src" "$dest"
}
CREDENTIALS_MANIFEST="${HARNESS_SEAT_CREDENTIALS_MANIFEST:-}"
[[ -n "$CREDENTIALS_MANIFEST" && -f "$CREDENTIALS_MANIFEST" ]] || { echo '{"ok":false,"detail":"seat-entrypoint: credential manifest missing from bundled runtime"}' >&2; exit 2; }
while IFS=$'\t' read -r cred_home cred_seed; do
  [[ -n "$cred_home" ]] && seed_copy "$cred_seed" "$HOME/$cred_home"
done < <(jq -r '.items[] | [.home, .seed] | @tsv' "$CREDENTIALS_MANIFEST")

export HARNESS_SEAT_CONTAINER=1
export TMPDIR=/tmp
[[ -n "$LOG_DIR" ]] && { mkdir -p "$LOG_DIR" || { echo '{"ok":false,"detail":"seat-entrypoint: log dir not writable"}' >&2; exit 2; }; export HARNESS_LOG_DIR="$LOG_DIR"; }

PROMPT="$(< /seed/prompt.txt)"

ARGS=("$WRAPPER" --workspace "$WORKSPACE" --trust "$PROMPT" --task-slug "$TASK_SLUG")
[[ -n "$MODEL" ]]     && ARGS+=(--model "$MODEL")
[[ -n "$TIMEOUT" ]]   && ARGS+=(--timeout "$TIMEOUT")
[[ -n "$PROFILE" ]]   && ARGS+=(--profile "$PROFILE")
[[ -n "$PERMISSION_MODE" ]] && ARGS+=(--permission-mode "$PERMISSION_MODE")
[[ -n "$RESUME" ]]    && ARGS+=(--resume "$RESUME")
[[ -n "$THREAD_ID" ]] && ARGS+=(--thread-id "$THREAD_ID")

cd "$WORKSPACE" || { echo '{"ok":false,"detail":"seat-entrypoint: cd workspace failed"}' >&2; exit 2; }
exec bash "${ARGS[@]}" < /dev/null
