File size: 3,681 Bytes
bd43184
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
#!/usr/bin/env python3
from __future__ import annotations

import argparse
import json
from collections import Counter
from pathlib import Path
from typing import Any


ROOT = Path(__file__).resolve().parents[1]
INVENTORY = ROOT / "data" / "opencode_tool_inventory.json"


def load_inventory() -> dict[str, dict[str, Any]]:
    blob = json.loads(INVENTORY.read_text())
    return {tool["name"]: tool for tool in blob["tools"]}


def iter_jsonl(path: Path):
    with path.open() as f:
        for line_no, line in enumerate(f, 1):
            if line.strip():
                yield line_no, json.loads(line)


def validate_training(path: Path, inventory: dict[str, dict[str, Any]]) -> list[str]:
    errors: list[str] = []
    coverage: Counter[str] = Counter()
    for line_no, row in iter_jsonl(path):
        messages = row.get("messages")
        if not isinstance(messages, list) or not messages:
            errors.append(f"{path}:{line_no}: missing messages")
            continue
        has_call = False
        for msg in messages:
            if msg.get("role") != "assistant":
                continue
            for tc in msg.get("tool_calls") or []:
                has_call = True
                fn = tc.get("function") or {}
                name = fn.get("name")
                if name not in inventory:
                    errors.append(f"{path}:{line_no}: unknown tool {name!r}")
                    continue
                coverage[name] += 1
                try:
                    args = json.loads(fn.get("arguments"))
                except Exception as exc:
                    errors.append(f"{path}:{line_no}: bad JSON arguments for {name}: {exc}")
                    continue
                if not isinstance(args, dict):
                    errors.append(f"{path}:{line_no}: {name} arguments must be an object")
                    continue
                for required in inventory[name].get("required", []):
                    if required not in args:
                        errors.append(f"{path}:{line_no}: {name} missing required argument {required}")
        if not has_call:
            errors.append(f"{path}:{line_no}: example has no assistant tool call")
    missing = sorted(set(inventory) - set(coverage))
    if missing:
        errors.append(f"{path}: missing opencode tool coverage: {', '.join(missing)}")
    return errors


def validate_eval(path: Path, inventory: dict[str, dict[str, Any]]) -> list[str]:
    errors: list[str] = []
    for line_no, row in iter_jsonl(path):
        if not row.get("id") or not row.get("prompt"):
            errors.append(f"{path}:{line_no}: eval row needs id and prompt")
        if not isinstance(row.get("files"), dict):
            errors.append(f"{path}:{line_no}: eval row needs files object")
        for name in row.get("expected_tools") or []:
            if name not in inventory:
                errors.append(f"{path}:{line_no}: unknown expected tool {name}")
        if not any(key in row for key in ["verify", "verify_contains", "verify_missing", "verify_command"]):
            errors.append(f"{path}:{line_no}: eval row has no verifier")
    return errors


def main() -> int:
    parser = argparse.ArgumentParser()
    parser.add_argument("path", type=Path)
    args = parser.parse_args()

    inventory = load_inventory()
    if args.path.name.endswith("_eval.jsonl"):
        errors = validate_eval(args.path, inventory)
    else:
        errors = validate_training(args.path, inventory)
    if errors:
        for error in errors:
            print(error)
        return 1
    print(f"ok: {args.path}")
    return 0


if __name__ == "__main__":
    raise SystemExit(main())