File size: 3,173 Bytes
837bf81
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
from __future__ import annotations

import argparse
import collections
import concurrent.futures
import hashlib
import json
import subprocess
from pathlib import Path

from jqweave.execution import run_jq


def jq_version() -> str:
    result = subprocess.run(["jq", "--version"], capture_output=True, text=True, check=False)
    return (result.stdout or result.stderr).strip()


def validate_row(row: dict) -> tuple[str, str | None]:
    try:
        if run_jq(row["command"], row["input_text"]) != row["expected_output"]:
            return row["id"], "primary output mismatch"
        if run_jq(row["command"], row["validation_input"]) != row["validation_output"]:
            return row["id"], "validation output mismatch"
        return row["id"], None
    except Exception as error:
        return row["id"], str(error)[:500]


def main() -> None:
    parser = argparse.ArgumentParser()
    parser.add_argument("data", type=Path)
    parser.add_argument("--workers", type=int, default=8)
    parser.add_argument("--output", type=Path)
    args = parser.parse_args()
    rows = [json.loads(line) for line in args.data.open(encoding="utf-8")]
    ids = [row["id"] for row in rows]
    if len(ids) != len(set(ids)):
        raise ValueError("Duplicate row IDs")
    errors = []
    with concurrent.futures.ProcessPoolExecutor(max_workers=args.workers) as executor:
        for completed, (row_id, error) in enumerate(executor.map(validate_row, rows, chunksize=100), 1):
            if error:
                errors.append({"id": row_id, "error": error})
            if completed % 5000 == 0:
                print(f"validated {completed}/{len(rows)}", flush=True)
    split_integrity = {}
    for left, right in (("train", "validation"), ("train", "test"), ("validation", "test")):
        left_rows = [row for row in rows if row["split"] == left]
        right_rows = [row for row in rows if row["split"] == right]
        split_integrity[f"{left}-{right}"] = {
            "input_overlap": len({row["input_text"] for row in left_rows} & {row["input_text"] for row in right_rows}),
            "request_command_overlap": len(
                {(row["request"], row["command"]) for row in left_rows}
                & {(row["request"], row["command"]) for row in right_rows}
            ),
        }
    summary = {
        "data": str(args.data),
        "sha256": hashlib.sha256(args.data.read_bytes()).hexdigest(),
        "rows": len(rows),
        "executions": len(rows) * 2,
        "jq_version": jq_version(),
        "errors": errors,
        "splits": dict(collections.Counter(row["split"] for row in rows)),
        "sources": dict(collections.Counter(row["source"] for row in rows)),
        "families": dict(collections.Counter(row["family"] for row in rows)),
        "unique_requests": len({row["request"] for row in rows}),
        "unique_commands": len({row["command"] for row in rows}),
        "split_integrity": split_integrity,
    }
    text = json.dumps(summary, indent=2)
    if args.output:
        args.output.write_text(text, encoding="utf-8")
    print(text)
    if errors:
        raise SystemExit(1)


if __name__ == "__main__":
    main()