#!/usr/bin/env python3
"""Rescore retained reasoning outputs after a fixture/extractor correction; no inference rerun."""

from __future__ import annotations

import argparse
import json
import re
from pathlib import Path

from harness_lib import answer_matches, dump, final_answer


def main() -> None:
    parser = argparse.ArgumentParser()
    parser.add_argument("--model-id", required=True)
    parser.add_argument("--root", type=Path, default=Path("/workspace/kingy-qwen38"))
    args = parser.parse_args()
    suite = json.loads((args.root / "fixtures/suite.json").read_text())
    answers = {row["id"]: row["answer"] for row in suite["reasoning"]}
    base = args.root / "results" / args.model_id / "reasoning"
    summary_rows = []
    for case_dir in sorted(path for path in base.iterdir() if path.is_dir()):
        runs = []
        for path in sorted(case_dir.glob("*.json")):
            item = json.loads(path.read_text())
            text = item.get("output") or ""
            final = final_answer(text)
            expected = answers[case_dir.name]
            format_compliant = bool(re.search(r"(?:^|\n)\s*FINAL\s*:\s*.+?\s*$", text, flags=re.I | re.M))
            answer_correct = answer_matches(final, expected)
            item.update({
                "passed": format_compliant and answer_correct,
                "answer_correct": answer_correct,
                "format_compliant": format_compliant,
                "expected": expected,
                "final": final,
                "rescore_note": "Corrected numeric label/unit matcher and rs_29 weekday key; inference output unchanged.",
            })
            dump(path, item)
            runs.append(item)
        summary_rows.append({"id": case_dir.name, "passes": sum(bool(row["passed"]) for row in runs)})
    dump(args.root / "results" / args.model_id / "reasoning-summary.json", {
        "passed_runs": sum(row["passes"] for row in summary_rows),
        "total_runs": len(summary_rows) * 3,
        "problems_any_pass": sum(row["passes"] > 0 for row in summary_rows),
        "problems": len(summary_rows),
        "rows": summary_rows,
        "rescore_note": "Retained outputs rescored after one fixture/extractor correction; no inference rerun.",
    })
    print(json.dumps({"model": args.model_id, "passed_runs": sum(row["passes"] for row in summary_rows), "problems_any_pass": sum(row["passes"] > 0 for row in summary_rows)}))


if __name__ == "__main__":
    main()
