#!/usr/bin/env bash
# chain_whole.sh FROZEN_DIR PREFIX ARM CACHE [TRACKHASH_REF] : (TRACKHASH_REF set => the sampler hook aborts a stage on a fingerprint mismatch, astra r8 finding 2; unset => capture only) whole-scroll two-stage twin of the fit 81 lineage (Va_<PREFIX>_s<S>_10k -> Vb_<PREFIX>_s<S>_25k), dataset arm dataset_tta_hub1030h_<ARM>, FIT_CACHE=$D/<CACHE> (2026-11-06; generalises chain_s3w.sh).
#   per seed: Va_${P}_s<S>_10k (10k scratch, loss_weight_shell_outer 0) -> Vb_${P}_s<S>_25k (25k refine from it), then scores_trim.json on the
#   trimmed records (as chain_t0.sh) and the sheet jump map; fail-fast (exit 10 fit, 11 scoring, 12 jump map). Stop file: work/chain_whole_${P}.stop.
set -u -o pipefail
D=/media/djosey/nvme4/spiral_0332; F=${1:?frozen dir}; P=${2:?prefix}; ARMX=${3:?dataset arm}; CACHE=${4:?cache dir name}; HREF=${5:-}; PY=/home/djosey/Documents/villa/spiral-fitting/.venv/bin/python
echo $$ > $D/work/chain_whole_${P}.pid
rm -f $D/work/chain_whole_${P}.stop
source <(sed -e '/chain_s1.pid/d' -e '/chain_dt done/d' -e '/^for ARM_TAG/,$d' $F/work/s1/chain_s1.sh) $F
export FIT_CACHE=$D/$CACHE PYTHONPATH=$D/work/density/hook
stop() { [ -f $D/work/chain_whole_${P}.stop ]; }
for S in 1 2; do
  stop && break
  export DENSITY_TRACK_HASH_OUT=$D/work/density/trackhash_Va_${P}_s${S}_10k.json; if [ -n "$HREF" ]; then export DENSITY_TRACK_HASH_REF=$HREF; else unset DENSITY_TRACK_HASH_REF; fi
  t0=$(date +%s); stage Va_${P}_s${S}_10k $ARMX 10000; rc=$?
  echo "$(date) Va_${P}_s${S}_10k wall $(( ($(date +%s)-t0)/60 )) min; $(grep -h 'DENSITY_TRACK_HASH' $D/work/fit_Va_${P}_s${S}_10k.log | tr '\n' ' ')"
  [ $rc -eq 0 ] || { echo "$(date) FAILED Va_${P}_s${S}_10k: stage returned $rc; chain aborts"; exit 10; }
  stop && break
  export DENSITY_TRACK_HASH_OUT=$D/work/density/trackhash_Vb_${P}_s${S}_25k.json
  t0=$(date +%s); stage Vb_${P}_s${S}_25k $ARMX 25000 Va_${P}_s${S}_10k; rc=$?
  echo "$(date) Vb_${P}_s${S}_25k wall $(( ($(date +%s)-t0)/60 )) min; $(grep -h 'DENSITY_TRACK_HASH' $D/work/fit_Vb_${P}_s${S}_25k.log | tr '\n' ' ')"
  [ $rc -eq 0 ] || { echo "$(date) FAILED Vb_${P}_s${S}_25k: stage returned $rc; chain aborts"; exit 10; }
  T=Vb_${P}_s${S}_25k; R=$(rundir $T)
  CUDA_VISIBLE_DEVICES= $PY $F/work/s1/s1_score.py $R --out $R/scores_trim.json \
    --train-records $D/work/s1_trim/anchors_train_records.json --held-records $D/work/s1_trim/anchors_held_records.json > $D/work/s1/scoretrim_$T.log 2>&1 \
    || { echo "$(date) FAILED $T: trimmed scoring failed; chain aborts"; exit 11; }
  echo "$(date) TSCORE $T $($PY -c "
import json; s=json.load(open('$R/scores_trim.json')); a=s['slice']['windows']['all']
print('f025 %.4f cens %.4f f05 %.4f off_model %s held_patches %.4f held_off %.4f' % (a['f025'], a['f025_cens'], a['f05'], a['off_model'], s['held_patches']['fraction'], s['anchors_held']['equal_fiber_off_rate']))")"
  mkdir -p $D/view/jumpmap_sheet
  CUDA_VISIBLE_DEVICES= $PY $D/work/fiber_jump_map_sheet.py $R $D/view/jumpmap_sheet/$T \
    --records $D/work/s1_trim/anchors_train_records.json $D/work/s1_trim/anchors_held_records.json > $D/work/s1/jumpmap_$T.log 2>&1 \
    || { echo "$(date) FAILED $T: jump map failed; chain aborts"; exit 12; }
  echo "$(date) jump map $T written"
done
echo "$(date) chain_whole ${P} done"
