#!/usr/bin/env bash
# ============================================================================
# Video-MME 900 自进化训练 —— 消费 video-split 冻结切分（capstone 训练）
# ----------------------------------------------------------------------------
# 自包含实验记录：写死全部参数，零参可复现（GPU 卡号除外）。
#
#   Phase 0: 建 adhoc-baseline seed（extract infer_adhoc 去重 baseline.db +
#            携带 video-split 冻结 pools.json / manifest；已存在则跳过）
#   Phase 1: train --fresh --seed adhoc-baseline（3 epochs，global 冻结切分）
#
# 用法（长时训练，建议 tmux 便于 attach 看实时进度）:
#   tmux new-session -d -s train_videomme "CUDA_VISIBLE_DEVICES=0 bash scripts/train_videomme.sh 2>&1 | tee logs/train_videomme.log"
#   或直接: CUDA_VISIBLE_DEVICES=0 bash scripts/train_videomme.sh
#
# 日志三重保险即时输出（绝不缓存）: PYTHONUNBUFFERED=1（环境级）+ python -u（命令级）
#   + loguru 走 stderr 同步写。tee 落盘不影响实时性。
#
# 前置（务必先做，见 research-wiki/reviews/2026-07-16-preflight-final-review.md）:
#   1. .env REDIS_CACHE_TTL 为正整数（0 会被 WP4 fail-loud 拒绝启动）
#   2. workspaces/video-split/pools.json 已冻结（build_video_split.sh 产出）
#   3. workspaces/default/harness.db 含 infer_adhoc 基线预测
# ============================================================================

set -euo pipefail

cd "$(dirname "$0")/.."

export CUDA_VISIBLE_DEVICES="${CUDA_VISIBLE_DEVICES:-0}"
export HF_HUB_OFFLINE=1
export TRANSFORMERS_OFFLINE=1
export PYTHONUNBUFFERED=1

set -a
source .env
set +a

PYTHON="$(conda run -n Video-Tree-TRM which python)"

SEED_NAME="adhoc-baseline"
SPLIT_DIR="workspaces/video-split"
BASELINE_DB="workspaces/default/harness.db"

# ── Phase 0: 建 seed（携带冻结 pools.json）──
if [[ ! -d "store/seeds/${SEED_NAME}" ]]; then
  echo "=== Phase 0: 建 seed ${SEED_NAME}（携带 video-split 冻结切分）==="
  "${PYTHON}" -u -c "
from pathlib import Path
import tempfile
from app.harness.store import extract_run_db, init_seed

split_dir = Path('${SPLIT_DIR}')
pools = split_dir / 'pools.json'
manifest = split_dir / 'split_manifest.json'
assert pools.exists(), f'冻结切分不存在: {pools}（先跑 build_video_split.sh）'

tmp = Path(tempfile.mkdtemp()) / 'baseline.db'
# canonical 每 question_id 取首行（902→900），对齐冻结切分口径
extract_run_db(Path('${BASELINE_DB}'), tmp, 'infer_adhoc', dedupe_per_question=True)
init_seed(
    store_dir=Path('store'),
    name='${SEED_NAME}',
    skills_dir=Path('store/skills/v1'),
    prompts_dir=Path('store/prompts/v1'),
    baseline_db=tmp,
    baseline_run_id='infer_adhoc',
    parent=None,
    description='Video-MME adhoc baseline + video-split 冻结三池（tier 感知 val_ratio=0.4）',
    pools_json=pools,
    split_manifest=manifest,
)
tmp.unlink()
print('Seed created: store/seeds/${SEED_NAME}/')
"
else
  echo "=== Phase 0: seed ${SEED_NAME} 已存在，跳过 ==="
fi

# ── Phase 1: Train ──
echo "=== Phase 1: Train (3 epochs, Video-MME 900 global 冻结切分) ==="
"${PYTHON}" -u main.py \
  --config config/train_videomme.yaml \
  --fresh \
  --seed "${SEED_NAME}"

echo "=== 训练完成 ==="
echo "结果查看:"
echo "  cat workspaces/train-videomme/analyses/final_test_eval.json"
echo "  sqlite3 workspaces/train-videomme/harness.db 'SELECT * FROM dual_metric'"
