#!/usr/bin/env python3
"""Small deterministic JSONL contract checker for held-out tool traces.

It checks syntax and call/result pairing only. It is not BFCL and does not
measure semantic correctness, latency, streaming or an actual tool runner.
"""
from __future__ import annotations

import argparse
import json
from pathlib import Path


def check(item: dict) -> list[str]:
    errors: list[str] = []
    calls: set[str] = set()
    for index, message in enumerate(item.get("messages", [])):
        role = message.get("role")
        if role not in {"system", "user", "assistant", "tool"}:
            errors.append(f"message {index}: invalid role {role!r}")
        for call in message.get("tool_calls", []):
            call_id = call.get("id")
            fn = call.get("function", {})
            if not call_id or fn.get("name") is None:
                errors.append(f"message {index}: malformed tool_call")
            try:
                json.loads(fn.get("arguments", ""))
            except json.JSONDecodeError:
                errors.append(f"message {index}: arguments are not JSON")
            calls.add(call_id)
        if role == "tool" and message.get("tool_call_id") not in calls:
            errors.append(f"message {index}: orphan tool result")
    return errors


def main() -> None:
    parser = argparse.ArgumentParser()
    parser.add_argument("jsonl", type=Path)
    args = parser.parse_args()
    total = bad = 0
    for line_number, line in enumerate(args.jsonl.read_text(encoding="utf-8").splitlines(), 1):
        if not line.strip():
            continue
        total += 1
        try:
            errors = check(json.loads(line))
        except json.JSONDecodeError as exc:
            errors = [f"line {line_number}: invalid JSON ({exc.msg})"]
        if errors:
            bad += 1
            print(f"row {line_number}: " + "; ".join(errors))
    print(json.dumps({"rows": total, "invalid_rows": bad, "pass_rate": (total - bad) / total if total else 0.0}))


if __name__ == "__main__":
    main()
