#!/usr/bin/env bash
set -euo pipefail
cd /home/agent/projects/CryptoTradingBot/Crypto_Agent
STRATS=(
  candidate_v62_loose_trend_rebound
  candidate_v62_scalp_fast_rebound
  candidate_v63_loose_fast_decay
  candidate_v64_squeeze_survival_strict
  candidate_v64_squeeze_survival_wide
  candidate_v65_signal_sampler_conservative
  candidate_v65_signal_sampler_aggressive
  v61_tight_survival
  v59_1_frequency_boost
  v60_survival
  candidate_v66_squeeze_breakout_sampler
  candidate_v69_squeeze_breakout_confirmed
  candidate_v73_multi_day_trend_investment
)

# Focused paper fleet: v67/v68 are intentionally excluded/blocked after negative scorecard.
# Paper-only: no live orders. CTB_PAPER_TRADING=true and CTB_DRY_RUN=false are set per strategy by multi_strategy_launcher.py.
.venv/bin/python multi_strategy_launcher.py \
  --runtime-dir /home/agent/.local/state/CryptoTradingBot \
  --max-parallel 13 \
  --strategies "${STRATS[@]}"

.venv/bin/python - <<'PY'
import os
import signal
import subprocess
from pathlib import Path
from multi_strategy_launcher import build_strategy_command_plan
from strategy_registry import load_strategy_presets

runtime_dir = Path('/home/agent/.local/state/CryptoTradingBot')
strategy_ids = '''candidate_v62_loose_trend_rebound candidate_v62_scalp_fast_rebound candidate_v63_loose_fast_decay candidate_v64_squeeze_survival_strict candidate_v64_squeeze_survival_wide candidate_v65_signal_sampler_conservative candidate_v65_signal_sampler_aggressive v61_tight_survival v59_1_frequency_boost v60_survival candidate_v66_squeeze_breakout_sampler candidate_v69_squeeze_breakout_confirmed candidate_v73_multi_day_trend_investment'''.split()
presets = load_strategy_presets()
plans = build_strategy_command_plan([presets[s] for s in strategy_ids], runtime_dir, max_parallel=13)
children = []

def stop(signum, frame):
    print(f"supervisor: received signal {signum}, terminating {len(children)} children", flush=True)
    for p in children:
        if p.poll() is None:
            p.terminate()
    for p in children:
        try:
            p.wait(timeout=10)
        except subprocess.TimeoutExpired:
            p.kill()
    raise SystemExit(128 + signum)

signal.signal(signal.SIGTERM, stop)
signal.signal(signal.SIGINT, stop)

for plan in plans:
    plan.log_path.parent.mkdir(parents=True, exist_ok=True)
    log_file = plan.log_path.open('a', encoding='utf-8')
    env = {**os.environ, **plan.env}
    proc = subprocess.Popen(plan.command, env=env, stdout=log_file, stderr=subprocess.STDOUT)
    plan.pid_path.write_text(str(proc.pid), encoding='utf-8')
    children.append(proc)
    print(f"supervisor: launched {plan.strategy_id} pid={proc.pid} log={plan.log_path}", flush=True)

exit_code = 0
while children:
    for proc in list(children):
        rc = proc.poll()
        if rc is not None:
            print(f"supervisor: child pid={proc.pid} exited rc={rc}", flush=True)
            exit_code = rc if exit_code == 0 else exit_code
            children.remove(proc)
    if children:
        signal.pause()
raise SystemExit(exit_code)
PY
