#!/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"

generals = [
    ln.strip()
    for ln in (V5/"signals"/"_meta"/"generals_roster.json").read_text().splitlines()
    if ln.strip() and not ln.strip().startswith("{")
]

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 GENERAL ASSIGNMENTS:", assigned)
print("OUTPUT:", flat_out)
