#!/usr/bin/env bash
# Contract probe: never resolves Codex; fake engine records exact wrapper argv.
set -euo pipefail
ROOT="$(cd "$(dirname "${BASH_SOURCE[0]}")/../../.." && pwd)"
tmp="$(mktemp -d)"; trap 'rm -rf "$tmp"' EXIT
mkdir -p "$tmp/work"
cat >"$tmp/engine" <<'EOF'
#!/usr/bin/env bash
printf '%s\0' "$@" >"$CAPTURE"
printf '%s\n' '{"type":"turn.completed"}'
EOF
chmod +x "$tmp/engine"
run() { CAPTURE="$tmp/$1" HARNESS_SEAT_CONTAINER=1 HARNESS_LOG_DIR="$tmp/logs" _CODEX_ENGINE_BIN="$tmp/engine" "$ROOT/modules/harness/wrappers/codex.sh" --workspace "$tmp/work" --trust prompt --task-slug incident-probe --model gpt-5.6-sol-high --permission-mode "$2" >/dev/null; }
run safe safe
CAPTURE="$tmp/ordinary" HARNESS_SEAT_CONTAINER=1 HARNESS_LOG_DIR="$tmp/logs" _CODEX_ENGINE_BIN="$tmp/engine" \
  "$ROOT/modules/harness/wrappers/codex.sh" --workspace "$tmp/work" --trust prompt --task-slug factory-probe --model gpt-5.6-sol-high >/dev/null
CAPTURE="$tmp/unsafe" HARNESS_SEAT_CONTAINER=1 HARNESS_LOG_DIR="$tmp/logs" _CODEX_ENGINE_BIN="$tmp/engine" \
  "$ROOT/modules/harness/wrappers/codex.sh" --workspace "$tmp/work" --trust prompt --task-slug factory-probe --model gpt-5.6-sol-high --permission-mode unsafe >/dev/null
python3 - "$tmp/ordinary" "$tmp/unsafe" <<'PY'
import sys
safe = open(sys.argv[1], "rb").read().split(b"\0")[:-1]
unsafe = open(sys.argv[2], "rb").read().split(b"\0")[:-1]
flag = b"--dangerously-bypass-approvals-and-sandbox"
assert flag not in safe, safe
sandbox = safe.index(b"--sandbox")
assert safe[sandbox + 1] == b"workspace-write", safe
assert safe.count(b"--sandbox") == 1, safe
assert unsafe.count(flag) == 1, unsafe
assert b"--sandbox" not in unsafe, unsafe
PY
python3 - "$tmp/safe" <<'PY'
import sys
safe = open(sys.argv[1], "rb").read().split(b"\0")[:-1]
flag = b"--dangerously-bypass-approvals-and-sandbox"
assert flag not in safe, safe
sandbox = safe.index(b"--sandbox")
approval = safe.index(b"--ask-for-approval")
assert safe[sandbox + 1] == b"workspace-write", safe
assert safe[approval + 1] == b"never", safe
assert safe.count(b"--sandbox") == 1, safe
assert safe.count(b"--ask-for-approval") == 1, safe
PY
CAPTURE="$tmp/profile" HARNESS_SEAT_CONTAINER=1 HARNESS_LOG_DIR="$tmp/logs" _CODEX_ENGINE_BIN="$tmp/engine" \
  "$ROOT/modules/harness/wrappers/codex.sh" --workspace "$tmp/work" --trust prompt --task-slug incident-probe --model gpt-5.6-sol-high --profile unsafe --permission-mode safe >/dev/null
python3 - "$tmp/profile" <<'PY'
import sys
args = open(sys.argv[1], "rb").read().split(b"\0")[:-1]
profile = args.index(b"--profile=unsafe")
exec_ = args.index(b"exec")
sandbox = args.index(b"--sandbox")
approval = args.index(b"--ask-for-approval")
assert profile < approval < exec_ < sandbox, args
assert args[sandbox + 1] == b"workspace-write", args
assert args[approval + 1] == b"never", args
assert b"--dangerously-bypass-approvals-and-sandbox" not in args, args
PY
refuse() {
  local name="$1"; shift
  local capture="$tmp/refuse-$name"
  rm -f "$capture"
  set +e
  CAPTURE="$capture" HARNESS_SEAT_CONTAINER=1 HARNESS_LOG_DIR="$tmp/logs" _CODEX_ENGINE_BIN="$tmp/engine" \
    "$ROOT/modules/harness/wrappers/codex.sh" "$@" >/dev/null 2>&1
  local rc=$?
  set -e
  [[ $rc -eq 2 ]] || { echo "expected usage refusal ($name): $*" >&2; exit 1; }
  [[ ! -e "$capture" ]] || { echo "engine launched on refusal ($name)" >&2; exit 1; }
}
refuse empty --workspace "$tmp/work" --trust prompt --task-slug probe --model gpt-5.6-sol-high --permission-mode ""
refuse unknown --workspace "$tmp/work" --trust prompt --task-slug probe --model gpt-5.6-sol-high --permission-mode nope
refuse incident-unsafe --workspace "$tmp/work" --trust prompt --task-slug incident-probe --model gpt-5.6-sol-high --permission-mode unsafe
refuse duplicate --workspace "$tmp/work" --trust prompt --task-slug probe --model gpt-5.6-sol-high --permission-mode safe --permission-mode unsafe
# Probes remain mode-exempt and use only the fake engine.
CAPTURE="$tmp/health" HARNESS_SEAT_CONTAINER=1 _CODEX_ENGINE_BIN="$tmp/engine" "$ROOT/modules/harness/wrappers/codex.sh" --health >/dev/null
CAPTURE="$tmp/models" HARNESS_SEAT_CONTAINER=1 _CODEX_ENGINE_BIN="$tmp/engine" "$ROOT/modules/harness/wrappers/codex.sh" --list-models >/dev/null
