#!/usr/bin/env bash
# Run the HDC-AM master fold to a 444.4 MiB bipolar fast-weights overlay.
set -euo pipefail
PY=${PY:-python}
FILE=${FILE:-fast_weights_master.safetensors}
SLOTS=$(( (444432242) / 4096 ))   # ~113,766 row budget (444.4 MiB, int8)

# 1) master fold (file corpus: fineweb-edu / wikipedia dumps exported as .txt)
$PY -X utf8 hdc_am_pipeline.py ingest \
  --d 4096 --slots "$SLOTS" --chunk-docs 512 --mode master \
  --glob "corpus/*.txt" --filename "$FILE"

echo "[gate-1] mmap + TR-1 strict bipolar"
$PY -X utf8 hdc_am_pipeline.py stats --overlay "$FILE"

echo "[gate-2] associative recall sanity"
$PY -X utf8 hdc_am_pipeline.py recall --overlay "$FILE" \
  --queries "Taj Mahal mausoleum Yamuna Agra" "BitNet ternary fast weights" \
  --topk 8

# confirm exact artifact size ≈ 444.4 MiB
SIZE_BYTES=$(stat -c%s "$FILE")
echo "[gate-3] artifact bytes = $SIZE_BYTES (target 465,987,174)"
echo "MASTER FOLD OK"