#!/usr/bin/env bash
set -uo pipefail
umask 077
readonly E="/home/cube/projects/richard/traning coach/.omo/evidence/task26/task26-gates18-20-activation-successor-30bcd6633875050aa4f56f49a8bb26cd616ffa2409fc04caef523a8a62d5d1cc-st_01a0080c"
readonly C="/home/cube/projects/richard/traning coach/.omo/evidence/task26/task26-activation-cutover-successor-30bcd6633875050aa4f56f49a8bb26cd616ffa2409fc04caef523a8a62d5d1cc"
readonly R="/home/cube/projects/richard/hermes-agent"
readonly PY="$R/.venv/bin/python"
readonly INSTALL="$E/artifacts/installed-wheel-root"
run_recorded() {
 local name="$1"; shift; local d="$E/artifacts/$name"; mkdir -p "$d"; chmod 700 "$d"
 printf '%q ' "$@" >"$d/command.txt"; printf '\n' >>"$d/command.txt"
 date --utc +%Y-%m-%dT%H:%M:%S.%NZ >"$d/started-at-utc.txt"; local s e rc pid; s=$(date +%s%N)
 "$@" >"$d/stdout.txt" 2>"$d/stderr.txt" & pid=$!; wait "$pid"; rc=$?; e=$(date +%s%N)
 date --utc +%Y-%m-%dT%H:%M:%S.%NZ >"$d/ended-at-utc.txt"; printf '%s\n' "$rc" >"$d/exit-code.txt"; printf '%s\n' "$(( (e-s)/1000000 ))" >"$d/duration-ms.txt"
 (cd "$d" && sha256sum command.txt started-at-utc.txt ended-at-utc.txt exit-code.txt duration-ms.txt stdout.txt stderr.txt >files.sha256)
 chmod 600 "$d"/*; chmod 700 "$d"; return 0
}
chmod -R u+X "$E/artifacts/installed-wheel-root" "$E/artifacts/parent-type-input" 2>/dev/null || true
cd "$R"
git status --porcelain=v1 -z >"$E/artifacts/git-status-before-retries.nul"
run_recorded installed-wheel-tests-retry bwrap --die-with-parent --unshare-net --ro-bind / / --bind "$E" "$E" --tmpfs /tmp --dev /dev --proc /proc --setenv HOME /tmp --setenv TMPDIR /tmp --setenv PYTHONDONTWRITEBYTECODE 1 --setenv PYTHONPATH "$INSTALL" --chdir /tmp "$PY" -B -m pytest -q -p no:cacheprovider --junitxml="$E/artifacts/installed-wheel-tests-retry.junit.xml" "$E/artifacts/wheel-tests/test_dualcoach_activation_cutover.py" "$E/artifacts/wheel-tests/test_dualcoach_admin_provider_auth.py" "$E/artifacts/wheel-tests/test_telegram_customer_bootstrap.py::test_customer_activation_commits_only_the_exact_bound_session"
readonly BUILD="$E/artifacts/reproducible-build-retry-output"
mkdir -p "$BUILD"
run_recorded reproducible-build-retry bwrap --die-with-parent --unshare-net --ro-bind / / --bind /home/cube/.cache/uv /home/cube/.cache/uv --bind "$BUILD" /tmp/wheel-out --tmpfs /tmp --dev /dev --proc /proc --setenv HOME /tmp --setenv TMPDIR /tmp --setenv UV_CACHE_DIR /home/cube/.cache/uv --chdir "$R" "$R/scripts/reproducible-wheel-build" "$R" /tmp/wheel-out
sha256sum "$BUILD"/*.whl >"$E/artifacts/reproducible-build-retry.sha256" 2>&1 || true
run_recorded canonical-order-regression bwrap --die-with-parent --unshare-net --ro-bind / / --bind "$E" "$E" --tmpfs /tmp --tmpfs "$R/.pytest-cache" --dev /dev --proc /proc --setenv HOME /home/cube --setenv TMPDIR /tmp --setenv PYTHONDONTWRITEBYTECODE 1 --chdir "$R" "$PY" -B -m pytest -q -p no:cacheprovider --junitxml="$E/artifacts/canonical-order-regression.junit.xml" tests/gateway/test_dualcoach_activation_cutover.py tests/gateway/test_nutrition_coaching.py::test_dualcoach_golden_path
run_recorded gate20-canonical-full "$PY" -B -m pytest -q -p no:cacheprovider --junitxml="$E/artifacts/gate20-canonical-full.junit.xml" tests/gateway
run_recorded candidate-final-verify "$PY" "$C/verify_candidate.py" "$C/verifier-input.json"
git status --porcelain=v1 -z >"$E/artifacts/git-status-after-retries.nul"
systemctl --user show hermes-gateway-dualcoachtest.service -p ActiveState -p SubState -p MainPID >"$E/artifacts/service-final.txt" 2>&1 || true
