#!/usr/bin/env python3
"""exp13 — слепая выкладка для АГЕНТ-ревью (ultracode, триангуляция к API-судьям).
Для каждой главы: source_zh.txt + A..E.txt (арм→метка перемешаны СВОЕЙ солью на главу).
Агенты ранжируют A..E, не зная моделей; un-blind — по _BLIND_KEY.json (держать закрытым).
Выход: diag/arms13/blind/ch{5,7,14}/{source_zh,A..E}.txt + diag/arms13/blind/_BLIND_KEY.json
"""
from __future__ import annotations
import json, random
from pathlib import Path

DIAG = Path("/home/ubuntu/books/gu-zhenren/diag")
ARMS = DIAG / "arms13"
BLIND = ARMS / "blind"
chunks = json.loads((DIAG / "chunks.json").read_text(encoding="utf-8"))
CH = [5, 7, 14]
CHUNKS = {5: [0, 1], 7: [0, 1], 14: [0, 1]}
SALT = "exp13-agentblind-v1"


def arms_present():
    return sorted([p.name for p in ARMS.iterdir() if p.is_dir() and p.name.startswith("T")])


def arm_text(arm, ch):
    return "\n\n".join((ARMS / arm / f"{ch}_{ci}.txt").read_text(encoding="utf-8").strip() for ci in CHUNKS[ch])


def source_text(ch):
    return "\n\n".join(chunks[f"{ch}/{ci}"]["source"] for ci in CHUNKS[ch])


def main():
    arms = arms_present()
    key = {}
    for ch in CH:
        order = arms[:]
        random.Random(f"{SALT}-{ch}").shuffle(order)
        labels = {chr(65 + i): arm for i, arm in enumerate(order)}   # A..E
        key[str(ch)] = labels
        d = BLIND / f"ch{ch}"; d.mkdir(parents=True, exist_ok=True)
        (d / "source_zh.txt").write_text(source_text(ch), encoding="utf-8")
        for lab, arm in labels.items():
            (d / f"{lab}.txt").write_text(arm_text(arm, ch), encoding="utf-8")
    (BLIND / "_BLIND_KEY.json").write_text(json.dumps({"salt": SALT, "map": key}, ensure_ascii=False, indent=1), encoding="utf-8")
    print(f"blind stage → {BLIND} ({len(arms)} меток A..{chr(64+len(arms))} × {len(CH)} глав)")
    for ch in CH:
        print(f"  гл.{ch}: {key[str(ch)]}")


if __name__ == "__main__":
    main()
