#!/usr/bin/env bash
set -euo pipefail
umask 077
PROJECT='/home/cube/projects/richard/traning coach';D=4a6c7ee54cf9526a30de8bb576c1d71b411938beba33a04914738f6e1b6ed1cb;E="$PROJECT/.omo/evidence/task26/task26-certification-deployment-$D-st_01a00b39";C="$PROJECT/.omo/evidence/task26/task26-customer-service-state-bootstrap-successor-$D";H=/home/cube/projects/richard/hermes-agent;P=/home/cube/.hermes/profiles/dualcoachtest;S="$P/workspace/checkin_cli";PY="$H/.venv/bin/python";TPY=/home/cube/miniconda3/bin/python;W="$C/artifacts/hermes_agent-0.17.0-py3-none-any.whl";FW="$PROJECT/.omo/evidence/task26/task26-inode-test-fix-successor-6aaef77de87489c3be493978645781b24b55ab07513ff1f880164741d0bd3c73/artifacts/physique_checkin_cli-0.1.0-py3-none-any.whl";mkdir -p "$E/artifacts" "$E/receipts"
record(){ n=$1;shift;o="$E/artifacts/$n";mkdir -p "$o";printf '%q ' "$@">"$o/command.txt";echo>>"$o/command.txt";date -u +%FT%T.%NZ>"$o/start.txt";set +e;"$@">"$o/stdout.txt" 2>"$o/stderr.txt";rc=$?;set -e;echo $rc>"$o/exit.txt";date -u +%FT%T.%NZ>"$o/end.txt";return $rc;}
inv(){ root=$1;out=$2;find "$root" -type f -not -path '*/__pycache__/*' -not -name '*.pyc' -printf '%P\0'|sort -z|while IFS= read -r -d '' f;do sha256sum "$root/$f";done >"$out";}
snap(){ t=$1;sha256sum "$P/customers/registry.json" "$P/data/onboarding/telegram-customer-bootstrap-v1/ledger.json" "$P/data/customers/task26_live_2e_r2_20260815_8527916639/nutrition-onboarding/readiness-receipt-v1.json" "$P/data/customers/task26_live_2e_r2_20260815_8527916639/nutrition-plans/feature-epoch.json">"$E/artifacts/authority-$t.sha256";inv "$P/data/customers/task26_live_2e_r2_20260815_8527916639" "$E/artifacts/customer-$t.sha256";inv "$S" "$E/artifacts/profile-workspace-$t.sha256";sha256sum "$H/.venv/lib/python3.12/site-packages/__editable__.physique_checkin_cli-0.1.0.pth" "$H/.venv/lib/python3.12/site-packages/__editable___physique_checkin_cli_0_1_0_finder.py" "$H/.venv/lib/python3.12/site-packages/physique_checkin_cli-0.1.0.dist-info/direct_url.json">"$E/artifacts/editable-$t.sha256";sha256sum "$P/config.yaml" "$P/.env">"$E/artifacts/provider-token-config-$t.sha256";sha256sum "$PROJECT/.omo/evidence/task26/task26-telegram-poll-recovery-st_01a00d2c/FINAL-SEAL.json" "$PROJECT/.omo/evidence/task26/task26-continuous-lifecycle-observer-v6.3-f38d0373/SEAL.json">"$E/artifacts/prior-seals-$t.sha256";ss="$P/data/owner-actions/customer-service-state.json";if [[ -e $ss ]];then stat -c '%a %s' "$ss">"$E/artifacts/service-state-$t.txt";sha256sum "$ss">>"$E/artifacts/service-state-$t.txt";else echo ABSENT>"$E/artifacts/service-state-$t.txt";fi;}
case ${1:-} in
 baseline)snap before;record candidate-verify "$C/verify_candidate.py" "$C";;
 gate18)record gate18-focused env -C "$H" HOME=/tmp PYTHONDONTWRITEBYTECODE=1 "$PY" -B -m pytest -q -p no:cacheprovider -o asyncio_default_fixture_loop_scope=function --junitxml="$E/artifacts/gate18-focused.junit.xml" tests/gateway/test_nutrition_coaching.py::test_coordinator_initializes_customer_service_state_store tests/gateway/test_nutrition_coaching.py::test_customer_dm_fallback_preserves_private_topic_route tests/gateway/test_telegram_polling_receipts.py::test_ptb_error_logs_safe_exception_metadata_without_content;record gate18-token env -C "$S" HOME=/tmp PYTHONDONTWRITEBYTECODE=1 "$PY" -B -m pytest -q -p no:cacheprovider --junitxml="$E/artifacts/gate18-token.junit.xml" tests/test_customer_admin.py -k activation_token_rotation_waiver;;
 gate19)if record gate19-predecessor bwrap --die-with-parent --unshare-net --ro-bind / / --bind "$E" "$E" --ro-bind "$C/snapshot/before/gateway/platforms/nutrition_coaching.py" "$H/gateway/platforms/nutrition_coaching.py" --tmpfs /tmp --dev /dev --proc /proc --setenv HOME /tmp --chdir "$H" "$PY" -B -m pytest -q -p no:cacheprovider -o asyncio_default_fixture_loop_scope=function --junitxml="$E/artifacts/gate19-predecessor.junit.xml" tests/gateway/test_nutrition_coaching.py::test_coordinator_initializes_customer_service_state_store;then exit 1;else [[ $? == 1 ]];fi;record gate19-successor env -C "$H" HOME=/tmp PYTHONDONTWRITEBYTECODE=1 "$PY" -B -m pytest -q -p no:cacheprovider -o asyncio_default_fixture_loop_scope=function --junitxml="$E/artifacts/gate19-successor.junit.xml" tests/gateway/test_nutrition_coaching.py tests/gateway/test_adaptive_nutrition.py tests/gateway/test_dualcoach_activation_cutover.py;;
 gate20)T=/tmp/task26-service-state-profile;rm -rf "$T";mkdir "$T";cp -a "$S"/. "$T"/;chmod -R u+rwX "$T";find "$T" -type d -name __pycache__ -prune -exec rm -rf {} +;record gate20-source-package env -C "$T" HOME=/tmp PYTHONPATH="$T" PYTHONDONTWRITEBYTECODE=1 "$TPY" -B -m pytest -q -p no:cacheprovider --junitxml="$E/artifacts/gate20-source-package.junit.xml" tests;record gate20-full-gateway env -C "$H" HOME=/home/cube PYTHONPATH="$H:$S" PYTHONDONTWRITEBYTECODE=1 "$PY" -B -m pytest -q -p no:cacheprovider -o asyncio_default_fixture_loop_scope=function --junitxml="$E/artifacts/gate20-full-gateway.junit.xml" tests/gateway;I="$E/artifacts/installed-hermes";mkdir "$I";record gate20-install "$PY" -m pip install --no-deps --no-compile --target "$I" "$W";record gate20-parity "$PY" - "$I" "$W" <<'PY'
import hashlib,json,re,sys,zipfile
from pathlib import Path
i,w=Path(sys.argv[1]),Path(sys.argv[2]);r=[]
with zipfile.ZipFile(w) as z:
 for n in sorted(x for x in z.namelist() if not x.endswith('/') and not x.endswith('.dist-info/RECORD')):
  p=i/re.sub(r'^hermes_agent-0\.17\.0\.data/data/','',n);r.append(p.is_file() and hashlib.sha256(p.read_bytes()).digest()==hashlib.sha256(z.read(n)).digest())
print(json.dumps({'members':len(r),'equal':sum(r),'status':'PASS' if all(r) else 'FAIL'}));raise SystemExit(not all(r))
PY
 X=/tmp/task26-installed-behavior;rm -rf "$X";mkdir -p "$X/tests/gateway";cp "$H/tests/gateway/test_nutrition_coaching.py" "$X/tests/gateway/";record gate20-installed-behavior env -C "$X" HOME=/tmp PYTHONPATH="$I:$S" PYTHONDONTWRITEBYTECODE=1 "$PY" -B -m pytest --import-mode=importlib -q -p no:cacheprovider -o asyncio_default_fixture_loop_scope=function --junitxml="$E/artifacts/gate20-installed-behavior.junit.xml" tests/gateway/test_nutrition_coaching.py::test_coordinator_initializes_customer_service_state_store;PI="$E/artifacts/installed-profile";mkdir "$PI";record gate20-profile-install "$PY" -m pip install --no-deps --no-compile --target "$PI" "$FW";record gate20-profile-tests env -C /tmp HOME=/tmp PYTHONPATH="$PI" PYTHONDONTWRITEBYTECODE=1 "$TPY" -B -m pytest -q -p no:cacheprovider --junitxml="$E/artifacts/gate20-profile-installed.junit.xml" "$T/tests/test_customer_admin.py";;
 after)snap after-gates;for x in authority customer profile-workspace editable provider-token-config prior-seals service-state;do cmp "$E/artifacts/$x-before."* "$E/artifacts/$x-after-gates."*;done;;*)exit 64;;esac
