"""sleeve_rv — CURATED relative-value sleeve: equal-weight of the 4 ortho books whose
marginal uplift to TP01 is POSITIVE AT EVERY hold-out cut (2022/23/24/25), i.e. persistent
rather than the 2025 ETH/BTC-bleed artifact. Two price-based + two implied-vol-based, so
the legs are mechanism-diverse (mutual corr < 0.6).

This is ONE executable 2-leg BTC/ETH book (the averaged legs stay within the $300/asset
cap because each sub-book is capped 0.5/leg and the mean of capped weights is capped).

  book(btc, eth) -> (w_btc, w_eth)
Use it as a small market-neutral satellite alongside TP01 (forward-monitor first).
"""
from __future__ import annotations

import importlib.util
import sys
from pathlib import Path

import numpy as np

HERE = Path(__file__).resolve().parent
sys.path.insert(0, str(HERE))

# the persistent-at-every-cut representatives (mechanism-diverse)
MEMBERS = [
    "agent_14_dvol_spread",
    "agent_04_ratio_donchian",
    "agent_03_relstrength_gated",
    "agent_15_vol_premium_tilt",
]


def _load(name: str):
    p = HERE / "agents" / f"{name}.py"
    spec = importlib.util.spec_from_file_location(name, p)
    mod = importlib.util.module_from_spec(spec)
    spec.loader.exec_module(mod)
    return mod.book


_BOOKS = [_load(n) for n in MEMBERS]


def book(btc, eth):
    wbs, wes = [], []
    for b in _BOOKS:
        wb, we = b(btc, eth)
        wbs.append(np.nan_to_num(np.asarray(wb, float)))
        wes.append(np.nan_to_num(np.asarray(we, float)))
    wb = np.mean(wbs, axis=0)
    we = np.mean(wes, axis=0)
    return np.clip(wb, -0.5, 0.5), np.clip(we, -0.5, 0.5)
