#!/usr/bin/env python3
import json
from pathlib import Path
from itertools import cycle

V5 = Path("/sovereign/chronological270/MASTER_STONE/GENESIS_V5")

flat_in  = V5 / "signals" / "flat_tagged_v5.jsonl"
flat_out = V5 / "signals" / "flat_tagged_v6.jsonl"

# ✅ Correctly load roster
roster = json.load(open(V5/"signals"/"_meta"/"generals_roster.json"))
generals = roster["generals"]

assert len(generals) == 33, f"Expected 33 generals, got {len(generals)}"

rot = cycle(generals)
assigned = 0

with flat_in.open() as f, flat_out.open("w") as w:
    for ln in f:
        o = json.loads(ln)

        if o.get("general") != "UNBOUND":
            w.write(json.dumps(o) + "\n")
            continue

        o["general"] = next(rot)
        o["assignment_mode"] = "forced_interpretive"
        assigned += 1

        w.write(json.dumps(o) + "\n")

print("FORCED ASSIGNMENTS:", assigned)
print("OUTPUT:", flat_out)
