#!/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;PY="$H/.venv/bin/python";W="$C/artifacts/hermes_agent-0.17.0-py3-none-any.whl";A="$E/artifacts/deployment";SS="$P/data/owner-actions/customer-service-state.json";mkdir -p "$A"
(cd "$PROJECT"&&sha256sum -c <(printf '%s  %s\n' 755b10c0cbbbb2db47dc3f15d499ae236ba9b62e68d37fa75175f2211cbe93fa "${E#$PROJECT/}/receipts/gate18-receipt.json";printf '%s  %s\n' 04edcf0e713986b66e9a675eed837357a02adb7bb6338b9c31e1e4b69d9382c5 "${E#$PROJECT/}/receipts/gate19-receipt.json";printf '%s  %s\n' 85d221807ca653a72279d1ba9d8f04ab66f96755cdb8f69fefbfd0ba3c8a6de2 "${E#$PROJECT/}/receipts/gate20-receipt.json"))>"$A/gate-preconditions.txt";"$C/verify_candidate.py" "$C">"$A/candidate-verify.json";[[ ! -e $SS ]]
cp "$E/artifacts/authority-after-gates.sha256" "$A/authority-before.sha256";cp "$E/artifacts/customer-after-gates.sha256" "$A/customer-before.sha256";cp "$E/artifacts/profile-workspace-after-gates.sha256" "$A/profile-workspace-before.sha256";cp "$E/artifacts/editable-after-gates.sha256" "$A/editable-before.sha256";cp "$E/artifacts/provider-token-config-after-gates.sha256" "$A/provider-token-config-before.sha256";cp "$E/artifacts/prior-seals-after-gates.sha256" "$A/prior-seals-before.sha256";sha256sum "$HOME/.config/systemd/user/hermes-gateway-dualcoachtest.service">"$A/unit-before.sha256"
START=$(date '+%Y-%m-%d %H:%M:%S');printf '%s\n' "$START">"$A/start-marker.txt";journalctl --user -u hermes-gateway-dualcoachtest.service -f -n0 --no-pager -o short-iso >"$A/journal-monitor.txt" 2>&1&JP=$!;echo "$JP">"$A/journal-monitor-armed.txt"
systemctl --user stop hermes-gateway-dualcoachtest.service;[[ $(systemctl --user show hermes-gateway-dualcoachtest.service -p MainPID --value) == 0 ]]
"$PY" -m pip install --no-deps --no-compile --force-reinstall "$W">"$A/pip-install.txt" 2>&1
"$PY" - "$W" "$P" >"$A/installed-proof.json" <<'PY'
import hashlib,json,sys
from pathlib import Path
w,p=Path(sys.argv[1]),Path(sys.argv[2]);import gateway.platforms.nutrition_coaching as n,checkin_cli
origin=Path(n.__file__).resolve();po=Path(checkin_cli.__file__).resolve();assert 'site-packages' in str(origin) and po.is_relative_to(p/'workspace/checkin_cli');du=json.loads((origin.parent.parent.parent/'hermes_agent-0.17.0.dist-info/direct_url.json').read_text());assert du['archive_info']['hashes']['sha256']==hashlib.sha256(w.read_bytes()).hexdigest();print(json.dumps({'wheel_sha256':hashlib.sha256(w.read_bytes()).hexdigest(),'module_origin':str(origin),'module_sha256':hashlib.sha256(origin.read_bytes()).hexdigest(),'direct_url':du,'profile_origin':str(po)},sort_keys=True,separators=(',',':')))
PY
systemctl --user start hermes-gateway-dualcoachtest.service
for _ in $(seq 1 90);do MP=$(systemctl --user show hermes-gateway-dualcoachtest.service -p MainPID --value);if [[ $MP -gt 1 ]]&&"$PY" - "$P/gateway_state.json" "$MP" <<'PY' >/dev/null 2>&1
import json,sys
x=json.load(open(sys.argv[1]));assert x['pid']==int(sys.argv[2]) and x['gateway_state']=='running' and x['platforms']['telegram']['state']=='connected'
PY
then break;fi;sleep 1;done
kill "$JP" 2>/dev/null||true;wait "$JP" 2>/dev/null||true
systemctl --user show hermes-gateway-dualcoachtest.service -p ActiveState -p SubState -p MainPID -p Result -p InvocationID -p ExecMainStartTimestamp >"$A/service.txt";grep -q '^ActiveState=active$' "$A/service.txt";grep -q '^SubState=running$' "$A/service.txt";MP=$(sed -n 's/^MainPID=//p' "$A/service.txt");cat "$P/gateway_state.json">"$A/gateway-state.json";"$PY" - "$A/gateway-state.json" "$MP" <<'PY'
import json,sys
x=json.load(open(sys.argv[1]));assert x['pid']==int(sys.argv[2]) and x['gateway_state']=='running' and x['platforms']['telegram']['state']=='connected'
PY
journalctl --user -u hermes-gateway-dualcoachtest.service --since "$START" --no-pager -o short-iso >"$A/current-journal.txt";grep -q 'Started hermes-gateway-dualcoachtest.service' "$A/current-journal.txt";! grep -E 'failed to connect|no connected platforms|nutrition_onboarding_callback_ingress|adaptive_plan_delivered|sent_audited|send to customer' "$A/current-journal.txt"
stat -c '%a %s %h %n' "$SS">"$A/service-state-stat.txt";[[ $(stat -c %a "$SS") == 600 ]];[[ $(stat -c %h "$SS") == 1 ]];sha256sum "$SS">"$A/service-state.sha256";cat "$SS">"$A/service-state.json"
(cd "$P"&&"$PY" - "$P" >"$A/live-transport-proof.json" <<'PY'
import hashlib,json,sys
from datetime import datetime
from pathlib import Path
from zoneinfo import ZoneInfo
from gateway.platforms.nutrition_coaching import IncomingAddress,NutritionCoachingCoordinator,load_committed_customer_registry
p=Path(sys.argv[1]);registry,rp=load_committed_customer_registry(p);c=NutritionCoachingCoordinator(p,registry,registry_path=Path(rp).resolve());today=datetime.now(ZoneInfo('Asia/Seoul')).date();key='task26_live_2e_r2_20260815_8527916639';address=IncomingAddress('8527916639','8527916639','0');allowed=c.customer_transport_allowed(key,address,kst_date=today);s=p/'data/owner-actions/customer-service-state.json';payload=json.loads(s.read_text());unsigned={'schema':payload['schema'],'states':payload['states']};digest=hashlib.sha256(json.dumps(unsigned,ensure_ascii=False,sort_keys=True,separators=(',',':')).encode()).hexdigest();assert allowed is True and payload=={'schema':'customer-service-state-v1','states':{},'payload_digest':digest};print(json.dumps({'customer_key':key,'route':{'user_id':'8527916639','chat_id':'8527916639','topic_id':'0'},'kst_date':today.isoformat(),'customer_transport_allowed':allowed,'service_state':payload,'service_state_sha256':hashlib.sha256(s.read_bytes()).hexdigest()},sort_keys=True,separators=(',',':')))
PY
)
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">"$A/authority-after.sha256";cmp "$A/authority-before.sha256" "$A/authority-after.sha256";find "$P/data/customers/task26_live_2e_r2_20260815_8527916639" -type f -printf '%P\0'|sort -z|while IFS= read -r -d '' f;do sha256sum "$P/data/customers/task26_live_2e_r2_20260815_8527916639/$f";done >"$A/customer-after.sha256";cmp "$A/customer-before.sha256" "$A/customer-after.sha256";find "$P/workspace/checkin_cli" -type f -not -path '*/__pycache__/*' -not -name '*.pyc' -printf '%P\0'|sort -z|while IFS= read -r -d '' f;do sha256sum "$P/workspace/checkin_cli/$f";done >"$A/profile-workspace-after.sha256";cmp "$A/profile-workspace-before.sha256" "$A/profile-workspace-after.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">"$A/editable-after.sha256";cmp "$A/editable-before.sha256" "$A/editable-after.sha256";sha256sum "$P/config.yaml" "$P/.env">"$A/provider-token-config-after.sha256";cmp "$A/provider-token-config-before.sha256" "$A/provider-token-config-after.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">"$A/prior-seals-after.sha256";cmp "$A/prior-seals-before.sha256" "$A/prior-seals-after.sha256";sha256sum "$HOME/.config/systemd/user/hermes-gateway-dualcoachtest.service">"$A/unit-after.sha256";cmp "$A/unit-before.sha256" "$A/unit-after.sha256"
"$PY" - "$P" >"$A/final-state.json" <<'PY'
import json,sys
from pathlib import Path
p=Path(sys.argv[1]);k='task26_live_2e_r2_20260815_8527916639';r=json.loads((p/'customers/registry.json').read_text());c=next(x for x in r['customers'] if x['customer_key']==k);b=json.loads((p/'data/onboarding/telegram-customer-bootstrap-v1/ledger.json').read_text());g=next(x for x in b['sessions'] if x['generation']==6);q=json.loads((p/'data/customers'/k/'nutrition-onboarding/readiness-receipt-v1.json').read_text());f=json.loads((p/'data/customers'/k/'nutrition-plans/feature-epoch.json').read_text());delivery=[p/'data/owner-actions/draft-deliveries.json',p/'data/scheduled-deliveries.jsonl'];assert c['enabled'] and g['state']=='ACTIVE' and not q['activation_enabled'] and not q['delivery_enabled'] and not f['activation'] and not f['delivery'] and not any(x.exists() for x in delivery);print(json.dumps({'customer_enabled':c['enabled'],'bootstrap_generation':6,'bootstrap_state':g['state'],'adaptive_activation':q['activation_enabled'],'adaptive_delivery':q['delivery_enabled'],'feature_activation':f['activation'],'feature_delivery':f['delivery'],'delivery_count':0},sort_keys=True,separators=(',',':')))
PY
ss -tnp >"$A/sockets.txt";grep "pid=$MP" "$A/sockets.txt"|grep -q ':443'
