#!/usr/bin/env bash
set -euo pipefail
umask 077
PROJECT='/home/cube/projects/richard/traning coach';D=f38d0373a58877806ff64a9cac54ab01e6e6e47f7101e3dcfd5ffbf05f8bb101;E="$PROJECT/.omo/evidence/task26/task26-certification-deployment-$D-st_01a00b39";C="$PROJECT/.omo/evidence/task26/task26-live-dm-checkin-fix-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-v2";mkdir -p "$A"
(cd "$PROJECT"&&sha256sum -c <(printf '%s  %s\n' d4f75024a418df8aaf575d248f8351b8f5be15654d6a769de87336fcdd899a5f "${E#$PROJECT/}/receipts/gate18-receipt.json";printf '%s  %s\n' 2fa802ca910a36168625859d1a8d7277656def6b80db85c180fc5ffa8d2ce2f3 "${E#$PROJECT/}/receipts/gate19-receipt.json";printf '%s  %s\n' 44d9b3f3129164ef3e01d751752976d07d61093861ace776a845f1bc8488c11b "${E#$PROJECT/}/receipts/gate20-receipt.json"))>"$A/gate-preconditions.txt"
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-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/state-after-gates.json" "$A/state-before.json"
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 asyncio,hashlib,io,json,logging,sys
from pathlib import Path
w,p=Path(sys.argv[1]).resolve(),Path(sys.argv[2]);import gateway.platforms.telegram as t,checkin_cli
origin=Path(t.__file__).resolve();profile_origin=Path(checkin_cli.__file__).resolve();assert 'site-packages' in str(origin) and profile_origin.is_relative_to(p/'workspace/checkin_cli')
class H(logging.Handler):
 def __init__(self):super().__init__();self.value=''
 def emit(self,r):self.value+=self.format(r)
h=H();log=logging.getLogger(t.__name__);log.addHandler(h);log.setLevel(logging.ERROR);a=object.__new__(t.TelegramAdapter);a._telegram_polling_receipt_gate=None
async def run():
 try:raise KeyError('sensitive-marker')
 except KeyError as e:await a._handle_ptb_error(type('U',(),{'update_id':1,'content':'private-marker'})(),type('C',(),{'error':e})())
asyncio.run(run());log.removeHandler(h);assert 'builtins.KeyError' in h.value and 'sensitive-marker' not in h.value and 'private-marker' not in h.value
du=json.loads((origin.parent.parent.parent/'hermes_agent-0.17.0.dist-info/direct_url.json').read_text());print(json.dumps({'hermes_origin':str(origin),'hermes_sha256':hashlib.sha256(origin.read_bytes()).hexdigest(),'direct_url':du,'wheel_sha256':hashlib.sha256(w.read_bytes()).hexdigest(),'profile_origin':str(profile_origin),'secure_error_log_test':'PASS'},sort_keys=True,separators=(',',':')))
PY
# Arm the required journal follower and application-log follower before start.
journalctl --user -u hermes-gateway-dualcoachtest.service -f -n0 --no-pager -o short-iso >"$A/journal-monitor.txt" 2>&1 & JP=$!
tail -n0 -F "$P/logs/gateway.log" >"$A/gateway-monitor.txt" 2>&1 & LP=$!
printf '%s %s\n' "$JP" "$LP">"$A/monitors-armed.txt"
systemctl --user start hermes-gateway-dualcoachtest.service
for i in $(seq 1 90);do grep -q 'Gateway running with 1 platform(s)' "$A/gateway-monitor.txt"&&break;sleep 1;done
grep -q '\[Telegram\] Connected to Telegram (polling mode)' "$A/gateway-monitor.txt";grep -q 'Gateway running with 1 platform(s)' "$A/gateway-monitor.txt"
kill "$JP" "$LP" 2>/dev/null||true;wait "$JP" "$LP" 2>/dev/null||true
systemctl --user show hermes-gateway-dualcoachtest.service -p ActiveState -p SubState -p MainPID -p Result -p InvocationID >"$A/service.txt";grep -q '^ActiveState=active$' "$A/service.txt";grep -q '^SubState=running$' "$A/service.txt"
"$PY" - "$P" >"$A/runtime-state.json" <<'PY'
import json,sys
from pathlib import Path
p=Path(sys.argv[1]);print((p/'gateway_state.json').read_text())
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"
"$PY" - "$P" >"$A/state-after.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());s=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());ds=[p/'data/owner-actions/draft-deliveries.json',p/'data/scheduled-deliveries.jsonl'];print(json.dumps({'customer_enabled':c['enabled'],'bootstrap_generation':s['generation'],'bootstrap_state':s['state'],'adaptive_activation':q['activation_enabled'],'adaptive_delivery':q['delivery_enabled'],'feature_activation':f['activation'],'feature_delivery':f['delivery'],'delivery_files_present':[str(x) for x in ds if x.exists()],'delivery_count':0},sort_keys=True,separators=(',',':')))
PY
cmp "$A/state-before.json" "$A/state-after.json"
# No customer delivery/callback evidence may appear in this invocation's startup monitor.
! grep -E 'adaptive_plan_delivered|nutrition.*callback|send to customer|delivery_pending|sent_audited' "$A/gateway-monitor.txt"
