"""Replay saved Jev trace decisions. No network calls or command execution."""
import csv
import json
from pathlib import Path

ACTIONS = {
    "Retry loop": "Inspect repeated arguments, timeouts and retry budget",
    "Missing input": "Identify and request the missing task fact",
    "Permission failure": "Check intended access with an authorized reviewer",
    "No failure shown": "Verify the full run and retain an audit sample",
    "Needs review": "Collect terminal statuses and inspect the full trace",
}

fixture = json.loads(Path(__file__).with_name("observed-traces.json").read_text())
rows = []
seen = set()
for sample in fixture["samples"]:
    if sample["id"] in seen:
        raise ValueError("Duplicate sample ID")
    seen.add(sample["id"])
    observed = sample["observed"]
    choice = observed["choice"]
    if choice not in ACTIONS or choice not in sample["request"]["choices"]:
        raise ValueError("Unknown decision label")
    probabilities = observed["probabilities"]
    if choice not in probabilities or any(not 0 <= p <= 1 for p in probabilities.values()):
        raise ValueError("Invalid probabilities")
    rows.append({"id": sample["id"], "decision": choice,
                 "probability": probabilities[choice], "next_check": ACTIONS[choice],
                 "reviewer_note": ""})

with Path(__file__).with_name("trace-review.csv").open("x", newline="", encoding="utf-8") as output:
    writer = csv.DictWriter(output, fieldnames=list(rows[0]))
    writer.writeheader()
    writer.writerows(rows)
print(f"Prepared {len(rows)} review rows. No tools were executed.")
